| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The following tasks guide you through using the PHP classes to copy an object within Amazon S3, from one bucket to another or to copy an object within the same bucket.
Copying Objects
1 | Create an instance of the |
2 | To copy an object, execute the If you want to copy several objects, you can optionally execute these requests in
parallel by adding the |
The following PHP code sample demonstrates the preceding tasks.
// Instantiate the class.
$s3 = new AmazonS3();
// 1. Copy one object.
$response = $s3->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
// 2. Copy objects using batch.
$s3->batch()->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
$s3->batch()->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
$responses = $s3->batch()->send();Example
The following PHP example illustrates the use of the copy_object()
method to copy the single object. First, the example copies a single object. The
example then illustrates the use of the batch operation, where it adds two
copy_object() requests to the batch, and later sends
the batch for parallel execution.
<?php
require_once '../aws-sdk-for-php/sdk.class.php';
$sourcebucket = '*** Source bucket ***';
$sourcekeyname = '*** Source object key name ***';
$targetbucket = '*** Target bucket ***';
$targetkeyname = '*** Target object key name ***';
// Instantiate the class.
$s3 = new AmazonS3();
// Copy object.
$response = $s3->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
// Success?
print_r($response->isOK());
// Add copy_object request to the batch to execute in parallel later.
$s3->batch()->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
$s3->batch()->copy_object(array( // Source.
'bucket' => $sourcebucket,
'filename' => $sourcekeyname
),
array( // Target.
'bucket' => $targetbucket,
'filename' => $targetkeyname
)
);
$responses = $s3->batch()->send();
if ($responses->areOK())
{
//...
}