0

我正在尝试将 S3 Multipart Uploader 从 Laravel 8 升级到 Laravel 9,并按照文档中的说明升级到 Flysystem 3,并且没有依赖错误https://laravel.com/docs/9.x/upgrade#flysystem-3 .

我无法访问底层 S3Client 来创建分段上传。

// 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 会抛出异常Call to undefined method League\Flysystem\Filesystem::getAdapter()

我查看了 League\Flysystem 的源代码和 Laravel 的更新,但似乎无法找出使用更新和访问底层Aws\S3\S3Client.

我更大的项目正在使用一个分叉的 laravel-uppy-s3-multipart-upload 库,可以在这里看到 https://github.com/kerkness/laravel-uppy-s3-multipart-upload/tree/laravel9

4

1 回答 1

1

这在 Flysystem AWS 适配器 github 问题中进行了讨论:

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

Laravel 中正在添加一个方法,将于下周二(2022 年 2 月 22 日)发布:

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

解决方法

LaravelFilesystemAdapter基类是可宏的,这意味着你可以在你的AppServiceProvider:

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

现在你可以打电话...

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

...您将拥有一个S3Client. (我喜欢宏!)

一旦下周二的版本可用,您就可以删除此宏。

于 2022-02-18T15:36:51.793 回答