3

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

   $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

有人有一个将复制对象复制到另一个存储桶的真实示例吗?

4

3 回答 3

3

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

  $response = $s3->copyObject(
        array(
            'Bucket' => 'DESTINATION BUCKET',
            'Key' => 'DESTINATION OBJECT KEY',
            'CopySource' => urlencode('SOURCE BUCKET' . '/' . 'SOURCE OBJECT KEY')
        )
    );    
于 2013-09-13T08:06:15.817 回答
0

你看过文档中的例子吗?

http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_copyObject

于 2013-09-13T02:06:46.403 回答
0

要将对象从一个存储桶复制到另一个存储桶,请执行以下步骤:
` require "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

于 2021-08-09T06:17:31.177 回答