简体   繁体   English

如何使用 php aws sdk 复制整个 s3 文件夹?

[英]How to copy whole s3 folders using php aws sdk?

I want copy a folder in aws s3 bucket.我想在 aws s3 存储桶中复制一个文件夹。 I am able to copy files but requirement is I should be able to copy whole s3 folder with some other name.我能够复制文件,但要求我应该能够使用其他名称复制整个 s3 文件夹。 example: if folder name is project1 I need to copy it as project1-copy in bucket called my_bucket .示例:如果文件夹名称为project1我需要将其作为project1-copy到名为my_bucket存储桶中。 I have tried below code.我试过下面的代码。

$s3->copyObject([
    'Bucket'     => my_bucket,
    'Key'        => "project-copy",
    'CopySource' => "my_bucket/project1",
]);

and also reverse as并且也反转为

$s3->copyObject([
    'Bucket'     => my_bucket,
    'Key'        => "project1",
    'CopySource' => "my_bucket/project-copy",
]);

In both case I am getting the error as在这两种情况下,我都收到错误消息

Error executing "CopyObject" on " https://s3.us-east-1.amazonaws.com/my_bucket/project1/ ";在“ https://s3.us-east-1.amazonaws.com/my_bucket/project1/ ”上执行“CopyObject”时出错; AWS HTTP error: Client error: PUT https://s3.us-east-1.amazonaws.com/my_bucket/project1/ resulted in a 404 Not Found response: NoSuchKeyThe specified key does not exist.34/F992013F6C3125 (truncated...) NoSuchKey (client): The specified key does not exist. AWS HTTP 错误:客户端错误: PUT https://s3.us-east-1.amazonaws.com/my_bucket/project1/导致404 Not Found响应:NoSuchKeyThe specified key does not exist.34/F992013F6C3125(截断.. .) NoSuchKey(客户端):指定的键不存在。 - NoSuchKeyThe specified key does not exist.34/F992013F6C312535YLrCcHhYJzimIKoR2knMvD3smhUbH75piDYTIx5SpKCPNkeWIifaYhKgezxnhpZzOHflzQngTnE=There was an error uploading - NoSuchKey 指定的键不存在。34/F992013F6C312535YLrCcHhYJzimIKoR2knMvD3smhUbH75piDYTIx5SpKCPNkeWIifaYhKgezxnhpZzOHflzQngTnE=上传出错

And I am sure of I am using right region and folder names also bucket name.而且我确信我使用了正确的区域和文件夹名称以及存储桶名称。 Still getting this above error.仍然收到上述错误。 How can I resolve this.?我该如何解决这个问题。?

Folders do not exist in Amazon S3. Amazon S3 中不存在文件夹。

Instead, the Key (filename) of an object includes the full path, eg:相反,对象的密钥(文件名)包括完整路径,例如:

s3merahkee/project1/foo.txt

If you wish to copy an entire 'folder', then you will need to copy all files objects within that path.如果您希望复制整个“文件夹”,则需要复制该路径内的所有文件对象。

Also, please note that you do not need to pre-create folders to store files.此外,请注意,你不需要预先创建文件夹来存储文件。 You could run a command like this:你可以运行这样的命令:

aws s3 cp foo.txt s3://my-bucket/s3merahkee/project1/foo.txt

This command would 'create' the s3merahkee and project1 folders.此命令将“创建” s3merahkeeproject1文件夹。 Well, actually it doesn't create them at all because they don't exist, but the Amazon S3 management console and even the aws s3 ls command will make it 'appear' as though those folders exist.好吧,实际上它根本不会创建它们,因为它们不存在,但是 Amazon S3 管理控制台甚至aws s3 ls命令都会使它“出现”,就好像这些文件夹存在一样。 If you were to delete the file, those folders would immediately disappear (because they don't actually exist).如果您要删除该文件,这些文件夹将立即消失(因为它们实际上并不存在)。

The easiest way to copy a whole folder is to use the AWS Command-Line Interface (CLI) , like this:复制整个文件夹的最简单方法是使用AWS 命令​​行界面 (CLI) ,如下所示:

aws s3 cp --recursive s3://my-bucket/folder1 s3://my-bucket/folder2

Here is explaned how to copy multiple objects within Amazon S3, from one bucket to another or within the same bucket using PHP SDK: https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingPHP.html下面解释了如何使用 PHP SDK 在 Amazon S3 中复制多个对象,从一个存储桶到另一个存储桶或在同一个存储桶中: https : //docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingPHP.html

If briefly, create a batch of operations form the list of files:如果简单的话,从文件列表中创建一批操作:

$batch[] = $s3->getCommand('CopyObject', [
    'Bucket'     => $targetBucket,
    'Key'        => "{targetKeyname}-{$i}",
    'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
]);

and send that batch with \\Aws\\CommandPool::batch($s3, $batch);并使用\\Aws\\CommandPool::batch($s3, $batch);发送该批次\\Aws\\CommandPool::batch($s3, $batch);

The accepted answer is correct in the sense that an object in S3 contains a full file path.接受的答案是正确的,因为 S3 中的对象包含完整的文件路径。 There is however a way to do this using the SDK so you don't need to use the command line.但是,有一种方法可以使用 SDK 执行此操作,因此您无需使用命令行。

AWS suggests using the Transfer object to do this: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-transfer.html AWS 建议使用 Transfer 对象来执行此操作: https : //docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-transfer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM