如何使用flysystem将文件从一个存储桶传输到另一个存储桶?

问题描述 投票:1回答:2

我在一个存储桶中有对象,我偶尔需要转移到Amazon S3中的第二个存储桶。我正在使用Laravel 5.3和Flysystem来管理这些存储桶。

一种解决方案是将图像下载到我的服务器,然后将其上传到另一个存储桶,但这似乎浪费时间/带宽,因为该文件存在于S3中并且正在S3内移动。这可以在Flysystem中完成,还是需要直接使用亚马逊的API?

laravel amazon-s3 laravel-5.3 flysystem
2个回答
-1
投票

您可以使用FilesystemAdapter移动功能移动文件:

$disk = Storage::disk('s3');
if (!$disk->move('bucketOne/testFile.jpg', 'bucketTwo/testFile.jpg')) {
   throw new \Exception('File could not be moved.');
}

1
投票

我找到了解决方案。这是我的代码:

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的客户端类,所以我在文档中发现,通过这个类,我可以将文件从一个桶移动到另一个桶。

S3 Php Documentation - Client Class - Copy Method

© www.soinside.com 2019 - 2024. All rights reserved.