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(
      '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.
    '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 ""; AWS HTTP error: Client error: PUT 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

While right aws bucket url is

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

In simple words, I wanted to copy the content of


  • 写回答

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:


    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

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



    • ¥18 如何用c++编写数学规律题
    • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
    • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
    • ¥20 怎样通过一个网址找到其他同样模版的网址
    • ¥30 XIAO esp32c3 读取FDC2214的数据
    • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
    • ¥100 webapi的部署(标签-服务器)
    • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
    • ¥15 C语言除0问题的检测方法