Laravel 8升级至9后getAdapter()报错,求官方解决方案
Laravel 9 中 AWS 预签名 URL 上传端点报错的官方解决方案
问题根源
Laravel 9 升级后,底层依赖的 Flysystem 从 2.x 版本升级到了 3.x,这个版本彻底移除了 getAdapter() 方法,导致你原来通过 getDriver()->getAdapter() 链式调用获取客户端和桶名的代码失效,抛出 Call to undefined method League\Flysystem\Filesystem::getAdapter() 错误。
官方替代方案
Laravel 针对 Flysystem 3.x 做了适配,提供了更直接的方式获取 S3 客户端和桶名,不需要再依赖废弃的 getAdapter():
- 获取 AWS S3 客户端:直接调用磁盘驱动的
getClient()方法 - 获取存储桶名称:从磁盘的配置数组中直接读取
bucket字段
修改后的完整代码
$s3 = Storage::disk("s3_attachments"); $postObject = new PostObjectV4( $s3->getDriver()->getClient(), $s3->getConfig()['bucket'], [ ["acl" => "public-read"], ["starts-with", "\$key", $key], ["starts-with", "\$Content-Type", $file_type], ], [["content-length-range", $file_size, $file_size]], "+1 hours" );
说明
这种方式是 Laravel 适配 Flysystem 3.x 后的官方推荐写法,完全替代了原来通过 getAdapter() 绕弯的逻辑,避免了使用宏定义这类临时兼容方案带来的潜在维护问题。
内容的提问来源于stack exchange,提问作者Matteo Robert
相关产品推荐
相关产品推荐

