Reputation: 79
I need to copy an object from a bucket to another bucket. I try with this code:
$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')
)
);
But I get an error type 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
Somebody have a real example of copyObject to another bucket?
Upvotes: 3
Views: 5419
Reputation: 1
To copy object from one bucket to another follow the steps:
` require "vendor/autoload.php";
use 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}"
]);
`
and refer the documentation of AWS sdk-3 for php documentation link: https://docs.aws.amazon.com/code-samples/latest/catalog/php-s3-s3-copying-objects.php.html
Upvotes: 0
Reputation: 79
Yes, I had looked at the documentation but I did not quite understand how to configure the "source" and "destination", but now I understand. Thanks!
$response = $s3->copyObject(
array(
'Bucket' => 'DESTINATION BUCKET',
'Key' => 'DESTINATION OBJECT KEY',
'CopySource' => urlencode('SOURCE BUCKET' . '/' . 'SOURCE OBJECT KEY')
)
);
Upvotes: 3
Reputation: 6945
Have you looked at the example in the docs?
http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_copyObject
Upvotes: 0