我在一个存储桶中有一些对象,偶尔需要转移到亚马逊S3的第二个存储桶中。我使用Laravel 5.3和Flysystem来管理这些存储桶。
一种解决方案是将图像下载到我的服务器,然后将其上传到另一个存储桶中,但这似乎是浪费时间/带宽,因为文件存在于S3中,并在S3中移动。这可以在Flysystem中完成吗?或者我需要直接使用Amazon的API吗?
发布于 2017-01-05 23:25:50
您可以使用FilesystemAdapter move函数来移动文件:
$disk = Storage::disk('s3');
if (!$disk->move('bucketOne/testFile.jpg', 'bucketTwo/testFile.jpg')) {
throw new \Exception('File could not be moved.');
}
发布于 2018-06-04 21:17:05
我已经找到了解决方案。下面是我的代码:
try {
$s3 = Storage::disk('s3');
$s3_temp = $s3->getDriver()->getAdapter()->getClient()->copy(
env('S3_BUCKET_ORIGIN'),
$file_path_origin,
env('S3_BUCKET_DEST'),
$file_path_dest,
'public-read'
);
} catch(\Exception $e) {
dd($e->getMessage());
}
记住,Laravel的S3文件系统使用SDK aws-s3-v3,因此,您可以搜索aws-s3-v3的库,看看Laravel包装器有哪些功能。
因此,在这个示例中,我获得了aws-s3的客户端类,因此我在文档中发现,使用这个类,我可以将文件从一个存储桶移动到另一个存储桶。
发布于 2019-09-27 15:41:01
对于Laravel > 5.6
try {
// creat two disk s3 and s3new
$fs = Storage::disk('s3')->getDriver();
$stream = $fs->readStream('file_path/1.jpg');
$new_fs = Storage::disk('s3new')->getDriver();
//will create new folder damages if not available
$new_fs->writeStream(
'damages/newfile.jpg',$stream
);
} catch(\Exception $e) {
dd($e->getMessage());
}
https://stackoverflow.com/questions/41479682
复制相似问题