简体   繁体   中英

Get S3Client from storage facade in Laravel 9

I am trying to upgrade an S3 Multipart Uploader from Laravel 8 to Laravel 9 and have upgraded to Flysystem 3 as outlined in the documentation and have no dependency errors https://laravel.com/docs/9.x/upgrade#flysystem-3 .

I am having trouble getting access to the underlying S3Client to create a Multipart upload.

// Working in Laravel 8
// Laravel 9 throws exception on getAdapter()

$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();

// Underlying S3Client is used to create Multipart uploader as below
$bucket = config('filesystems.disks.s3.bucket');
$result = $client->createMultipartUpload([
    'Bucket' => $bucket,
    'Key' => $key,
    'ContentType' => 'image/jpeg',
    'ContentDisposition' => 'inline',
]);

return response()
    ->json([
        'uploadId' => $result['UploadId'],
        'key' => $result['Key'],
    ]);

Laravel 9, however, throws an exception Call to undefined method League\Flysystem\Filesystem::getAdapter() .

I've looked over the source for League\Flysystem and updates to Laravel but can't seem to figure out the right way to work with the updates and get access to the underlying Aws\S3\S3Client .

My larger project is using a forked laravel-uppy-s3-multipart-upload library which can be seen here https://github.com/kerkness/laravel-uppy-s3-multipart-upload/tree/laravel9

This was discussed in this Flysystem AWS adapter github issue:

https://github.com/thephpleague/flysystem-aws-s3-v3/issues/284

A method is being added in Laravel, and will be released next Tuesday (February 22, 2022):

https://github.com/laravel/framework/pull/41079

Workaround

The Laravel FilesystemAdapter base class is macroable, which means you could do this in your AppServiceProvider :

Illuminate\Filesystem\AwsS3V3Adapter::macro('getClient', fn() => $this->client);

Now you can call...

Storage::disk('s3')->getClient();

... and you will have an instance of the S3Client . (I love macros!)

You can remove this macro once next Tuesday's release is available.

Get / Fetch object / Image from S3 in Laravel 9? OR *** Display files from S3 in Laravel ? ***

Ans:

These are only three steps to get object like ( Image, PDF, Video ) any kind of attachment from Laravel 9:

Go to project directory:

Step #1: composer require league/flysystem-aws-s3-v3

Step #2 Go to .env and right this

  • AWS_ACCESS_KEY_ID=your_aws_access_key

    AWS_SECRET_ACCESS_KEY=your_aws_secret_key

    AWS_DEFAULT_REGION=your_aws_regios

    AWS_BUCKET=your_bucket_name

    AWS_USE_PATH_STYLE_ENDPOINT=false

Step #3: Goto any controller and right this in the function $source = Storage::disk('s3')->temporaryUrl($item->path, now()->addMinutes(30));

And then you can pass $source variable to wherever you want.

Thanks

Jahanzaib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM