Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Copy an Object Using the AWS SDK for PHP

This topic guides you through using classes from the AWS SDK for PHP to copy a single object and multiple objects within Amazon S3, from one bucket to another or within the same bucket.

Note

This topic assumes that you are already following the instructions for Using the AWS SDK for PHP and Running PHP Examples and have the AWS SDK for PHP properly installed.

The following tasks guide you through using PHP SDK classes to copy an object that is already stored in Amazon S3.

Copying an Object

1

Create an instance of an Amazon S3 client by using the Aws\S3\S3Client class factory() method.

2

To copy an object, execute the Aws\S3\S3Client::copyObject() method. You need to provide information such as source bucket, source key name, target bucket, and target key name.

The following PHP code sample demonstrates using the copyObject() method to copy an object that is already stored in Amazon S3.

Copy
use Aws\S3\S3Client; $sourceBucket = '*** Your Source Bucket Name ***'; $sourceKeyname = '*** Your Source Object Key ***'; $targetBucket = '*** Your Target Bucket Name ***'; $targetKeyname = '*** Your Target Key Name ***'; // Instantiate the client. $s3 = S3Client::factory(); // Copy an object. $s3->copyObject(array( 'Bucket' => $targetBucket, 'Key' => $targetKeyname, 'CopySource' => "{$sourceBucket}/{$sourceKeyname}", ));

The following tasks guide you through using PHP classes to make multiple copies of an object within Amazon S3.

Copying Objects

1

Create an instance of an Amazon S3 client by using the Aws\S3\S3Client class factory() method.

2

To make multiple copies of an object, you execute a batch of calls to the Amazon S3 client getCommand() method, which is inherited from the Guzzle\Service\Client class. You provide the CopyObject command as the first argument and an array containing the source bucket, source key name, target bucket, and target key name as the second argument.

The following PHP code sample demonstrates making multiple copies of an object that is stored in Amazon S3.

Copy
use Aws\S3\S3Client; $sourceBucket = '*** Your Source Bucket Name ***'; $sourceKeyname = '*** Your Source Object Key ***'; $targetBucket = '*** Your Target Bucket Name ***'; $targetKeyname = '*** Your Target Key Name ***'; // Instantiate the client. $s3 = S3Client::factory(); // Perform a batch of CopyObject operations. $batch = array(); for ($i = 1; $i <= 3; $i++) { $batch[] = $s3->getCommand('CopyObject', array( 'Bucket' => $targetBucket, 'Key' => "{targetKeyname}-{$i}", 'CopySource' => "{$sourceBucket}/{$sourceKeyname}", )); } try { $successful = $s3->execute($batch); $failed = array(); } catch (\Guzzle\Service\Exception\CommandTransferException $e) { $successful = $e->getSuccessfulCommands(); $failed = $e->getFailedCommands(); }

Example of Copying Objects within Amazon S3

The following PHP example illustrates the use of the copyObject() method to copy a single object within Amazon S3 and using a batch of calls to CopyObject using the getcommand() method to make multiple copies of an object.

Copy
<?php // Include the AWS SDK using the Composer autoloader. require 'vendor/autoload.php'; use Aws\S3\S3Client; $sourceBucket = '*** Your Source Bucket Name ***'; $sourceKeyname = '*** Your Source Object Key ***'; $targetBucket = '*** Your Target Bucket Name ***'; // Instantiate the client. $s3 = S3Client::factory(); // Copy an object. $s3->copyObject(array( 'Bucket' => $targetBucket, 'Key' => "{$sourceKeyname}-copy", 'CopySource' => "{$sourceBucket}/{$sourceKeyname}", )); // Perform a batch of CopyObject operations. $batch = array(); for ($i = 1; $i <= 3; $i++) { $batch[] = $s3->getCommand('CopyObject', array( 'Bucket' => $targetBucket, 'Key' => "{$sourceKeyname}-copy-{$i}", 'CopySource' => "{$sourceBucket}/{$sourceKeyname}", )); } try { $successful = $s3->execute($batch); $failed = array(); } catch (\Guzzle\Service\Exception\CommandTransferException $e) { $successful = $e->getSuccessfulCommands(); $failed = $e->getFailedCommands(); }

Related Resources

On this page: