我正在尝试复制已经在s3上的文件夹,并在laravel 5.4中的S3上以不同的名称保存它。到目前为止我发现的是我可以复制一个Image,Not文件夹。我试图复制文件夹,如:
$disk->copy("admin/form/$old_form","admin/form/$new_form");
但它并不像那样。它给了我一个错误。我是否需要应用循环并分别获取每个文件夹项?像:
$images = $disk->allFiles('admin/form/$id');
或者在laravel或者s3 api中有没有可用的工作?
请帮助,它让我疯狂。
提前致谢。
答案 0 :(得分:3)
我正在做同样的事情。根据我到目前为止所读到的内容,使用Laravel复制目录本身似乎不可能。我到目前为止所提出的建议建议查看和复制每张图片,但我对速度并不满意(因为我每天都会在很多图像上做几次)。
请注意,我只是像这样直接使用Filesystem,所以我可以更轻松地访问PHP Storm中的方法。 $s3 = \Storage::disk('s3');
将完成与我的前两行相同的事情。如果我发现任何有效的方法,我会更新这个答案。
$filesystem = new FilesystemManager(app());
$s3 = $filesystem->disk('s3');
$images = $s3->allFiles('old-folder');
$s3->deleteDirectory('new_folder'); // If the file already exists, it will throw an exception. In my case I'm deleting the entire folder to simplify things.
foreach($images as $image)
{
$new_loc = str_replace('old-folder', 'new-folder', $image);
$s3->copy($image, $new_loc);
}
答案 1 :(得分:1)
另一种选择:
$files = Storage::disk('s3')->allFiles("old/location");
foreach($files as $file){
$copied_file = str_replace("old/location", "new/location", $file);
if(!Storage::disk('s3')->exists($copied_file)) Storage::disk('s3')->copy($file, $copied_file);
}
答案 2 :(得分:1)
这一切都可以通过CLI完成:
安装和配置s3cmd
:
sudo apt install s3cmd
s3cmd --configure
然后您可以:
s3cmd sync --delete-removed path/to/folder s3://bucket-name/path/to/folder/
要公开文件和文件夹:
s3cmd setacl s3://bucket-name/path/to/folder/ --acl-public --recursive
答案 3 :(得分:0)
我发现一种更快的方法是利用aws command line tools,特别是aws s3 sync
命令。
一旦安装在系统上,您就可以使用shell_exec
在Laravel项目中调用-例如:
$source = 's3://abc';
$destination = 's3://xyz';
shell_exec('aws s3 sync ' . $source . ' ' . $destination);
如果您在.env文件中设置了AWS_KEY
和AWS_SECRET
,则在Laravel中调用aws
命令时将引用这些值。