douzhi19900102 2019-05-21 18:33
浏览 137
已采纳

在php中的aws s3中将文件夹从一个复制到另一个

I am trying to copy a folder to another in aws s3 as below

$s3 = S3Client::factory(
    array(
      'credentials' => array(
        'key' => 'testbucket',
        'secret' => BUCKET_SECRET //Global constant
      ),
      'version' => BUCKET_VERSION, //Global constant
      'region'  => BUCKET_REGION  //Global constant
    )
  );
$sourceBucket = 'testbucket';
$sourceKeyname = 'admin/collections/Athena'; // Object key
$targetBucket = 'testbucket';
$targetKeyname = 'admin/collections/Athena-New';

// Copy an object.
$s3->copyObject(array(
    'Bucket'     => $targetBucket,
    'Key'        => $targetKeyname,
    'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
));

It is throwing error as

Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "CopyObject" on "https://testbucket.s3.us-east-2.amazonaws.com/admin/collections/Athena-New"; AWS HTTP error: Client error: PUT https://testbucket.s3.us-east-2.amazonaws.com/admin/collections/Athena-New resulted in a 404 Not Found response: NoSuchKeyThe specified key does not exist.admin/collections/AthenaNoSuchKeyThe specified key does not exist.admin/collections/Athena29EA131A5AD9CB836OjDNLgbdLPLMd0t7MuNi4JH6AU5pKfRmhCcWigGAaTuRlqoX8X5aMicWTui56rTH1BLRpJJtmc='

I can't figure out why it is making wrong bucket url like

https://testbucket.s3.us-east-2.amazonaws.com/admin/collections/Athena-New

While right aws bucket url is

https://s3.us-east-2.amazonaws.com/testbucket/admin/collections/Athena-New

Why it is appending the bucket name to before s3 in url?

In simple words, I wanted to copy the content of

https://s3.us-east-2.amazonaws.com/testbucket/admin/collections/Athena

to

https://s3.us-east-2.amazonaws.com/testbucket/admin/collections/Athena-New

  • 写回答

1条回答 默认 最新

  • doushi1900 2019-05-21 23:34
    关注

    It is not possible to "copy a folder" in Amazon S3 because folders do not actually exist.

    Instead, the full path of an object is stored in the object's Key (filename).

    So, an object might be called:

    admin/collections/Athena/foo.txt
    

    If you wish to copy all objects from one "folder" to another "folder", then you will need to:

    • Obtain a listing of the bucket for the given Prefix (effectively, full path to the folder)
    • Loop through each object returned, and copy the objects one-at-a-time to the new name (which effectively puts it in a new folder)

    So, it would copy admin/collections/Athena/foo.txt to admin/collections/Athena-New/foo.txt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元