AWS S3 PHP SDK 2将对象复制到另一个存储桶错误“错误请求”400

时间:2013-09-12 17:10:27

标签: php amazon-s3

我需要将一个对象从一个桶复制到另一个桶。我尝试使用此代码:

   $response = $s3->copyObject(
            array(
                'Bucket' => 'ORIGINAL BUCKET',
                'Key' => 'OBJECT KEY',
                'CopySource' => urlencode('ORIGINAL BUCKET' . '/' . 'OBJECT KEY')
            ), array(
                'Bucket' => 'NEW BUCKET',
                'Key' => 'NEW OBJECT KEY',
                'CopySource' => urlencode('NEW BUCKET' . '/' . 'NEW OBJECT KEY')
            )

        );      

但是我收到错误类型400 Bad Request:

object(Aws\S3\Exception\InvalidRequestException)[274]
  protected 'response' => 
    object(Guzzle\Http\Message\Response)[261]
      protected 'body' => 
        object(Guzzle\Http\EntityBody)[260]
          protected 'contentEncoding' => boolean false
          protected 'rewindFunction' => null 
          protected 'stream' => resource(299, stream)
          protected 'size' => null
          protected 'cache' => 
            array (size=9)
              ...
          protected 'customData' => 
            array (size=0)
              ...
      protected 'reasonPhrase' => string 'Bad Request' (length=11)
      protected 'statusCode' => int 400

有人将copyObject的一个真实示例带到另一个存储桶吗?

3 个答案:

答案 0 :(得分:3)

是的,我查看了文档,但我不太了解如何配置“源”和“目的地”,但现在我明白了。谢谢!

  $response = $s3->copyObject(
        array(
            'Bucket' => 'DESTINATION BUCKET',
            'Key' => 'DESTINATION OBJECT KEY',
            'CopySource' => urlencode('SOURCE BUCKET' . '/' . 'SOURCE OBJECT KEY')
        )
    );    

答案 1 :(得分:0)

答案 2 :(得分:0)

要将对象从一个存储桶复制到另一个存储桶,请按照以下步骤操作:
` 需要“vendor/autoload.php”; 使用 Aws\S3\S3Client;

$s3Client = new S3Client([
    'profile' => 'default',
    'region' => 'us-west-1',
    'version' => 'latest',
 ]);

$DestinationBucketFolderName = "NewFolder1";
$sourceBucket = '*** Your Source Bucket Name ***';
$sourceKeyname = '*** Your Source Object Key / Object Name That Are 
Stored On Source Bucket ***';
$targetBucket = '*** Your Target Bucket Name / Destination Bucket 
Name ***';

// Copy an object.
 $s3Client->copyObject([
      'Bucket'     => $targetBucket,
      'Key'        => " 
      {$DestinationBucketFolderName}/{$sourceKeyname}",
      'CopySource' => "{$sourceBucket}/{$sourceKeyname}"
]);

`

并参考 AWS sdk-3 的 php 文档 文档链接:https://docs.aws.amazon.com/code-samples/latest/catalog/php-s3-s3-copying-objects.php.html