Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Copy an Object Using the AWS SDK for PHP

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 AmazonS3 class by providing your AWS credentials.

2

To copy an object, execute the AmazonS3::copy_object() low-level method. You need to provide information such as source bucket, source key name, target bucket, and target key name. You provide this information as a ComplexType parameter, where a ComplexType is a set of key-value pairs.

If you want to copy several objects, you can optionally execute these requests in parallel by adding the copy_object requests to the batch queue and executing the batch by calling the batch.send() method.


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())
{
    //...
}