2

I have objects in one bucket that I occasionally need to transfer to a second bucket in Amazon S3. I'm using Laravel 5.3 with Flysystem to manage those buckets.

One solution is to download the images to my server and then upload it to the other bucket but this seems like a waste of time/bandwidth since the file exists in S3 and is getting moved within S3. Can this be done within Flysystem or will I need to directly use Amazon's API?

4

3 回答 3

6

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

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 文档 - 客户端类 - 复制方法

于 2018-06-04T13:17:05.430 回答
-1

对于 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());
    }
于 2019-09-27T07:41:01.257 回答
-2

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

$disk = Storage::disk('s3');
if (!$disk->move('bucketOne/testFile.jpg', 'bucketTwo/testFile.jpg')) {
   throw new \Exception('File could not be moved.');
}
于 2017-01-05T15:25:49.723 回答