Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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. You can remove this macro once next Tuesday’s release is available.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading