AWS Developer Tools Blog
Syncing Data with Amazon S3
Warning: this blog post provides instructions for AWS SDK for PHP V2, if you are looking for AWS SDK for PHP V3 instructions, please see our SDK guide.
Have you ever needed to upload an entire directory of files to Amazon S3 or download an Amazon S3 bucket to a local directory? With a recent release of the AWS SDK for PHP, this is now not only possible, but really simple.
Uploading a directory to a bucket
First, let’s create a client object that we will use in each example.
use AwsS3S3Client; $client = S3Client::factory(array( 'key' => 'your-aws-access-key-id', 'secret' => 'your-aws-secret-access-key' ));
After creating a client, you can upload a local directory to an Amazon S3 bucket using the uploadDirectory()
method of a client:
$client->uploadDirectory('/local/directory', 'my-bucket');
This small bit of code compares the contents of the local directory to the contents in the Amazon S3 bucket and only transfer files that have changed. While iterating over the keys in the bucket and comparing against the names of local files, the changed files are uploaded in parallel using batches of requests. When the size of a file exceeds a customizable multipart_upload_size
option, the uploader automatically uploads the file using a multipart upload.
Customizing the upload sync
Plenty of options and customizations exist to make the uploadDirectory()
method flexible so that it can fit many different use cases and requirements.
The following example uploads a local directory where each object is stored in the bucket using a public-read
ACL, 20 requests are sent in parallel, and debug information is printed to standard output as each request is transferred.
$dir = '/local/directory'; $bucket = 'my-bucket'; $keyPrefix = ''; $options = array( 'params' => array('ACL' => 'public-read'), 'concurrency' => 20, 'debug' => true ); $client->uploadDirectory($dir, $bucket, $keyPrefix, $options);
By specifying $keyPrefix
, you can cause the uploaded objects to be placed under a virtual folder in the Amazon S3 bucket. For example, if the $bucket
name is “my-bucket” and the $keyPrefix
is “testing/”, then your files will be uploaded to “my-bucket” under the “testing/” virtual folder: https://my-bucket.s3.amazonaws.com/testing/filename.txt
.
You can find more documentation about uploading a directory to a bucket in the AWS SDK for PHP User Guide.
Downloading a bucket
Downloading an Amazon S3 bucket to a local directory is just as easy. We’ll again use a simple function available on an AwsS3S3Client
object to easily download objects: downloadBucket()
.
The following example downloads all of the objects from my-bucket
and stores them in /local/directory
. Object keys that are under virtual subfolders are converted into a nested directory structure when the objects are downloaded.
$client->downloadBucket('/local/directory', 'my-bucket');
Customizing the download sync
Similar to the uploadDirectory()
method, the downloadBucket()
method has several options that can customize how files are downloaded.
The following example downloads a bucket to a local directory by downloading 20 objects in parallel and prints debug information to standard output as each transfer takes place.
$dir = '/local/directory'; $bucket = 'my-bucket'; $keyPrefix = ''; $client->downloadBucket($dir, $bucket, $keyPrefix, array( 'concurrency' => 20, 'debug' => true ));
By specifying $keyPrefix
, you can limit the downloaded objects to only keys that begin with the specified $keyPrefix
. This can be useful for downloading objects under a virtual directory.
The downloadBucket()
method also accepts an optional associative array of $options
that can be used to further control the transfer. One option of note is the allow_resumable
option, which allows the transfer to resume any previously interrupted downloads. This can be useful for resuming the download of a very large object so that you only need to download any remaining bytes.
You can find more documentation on syncing buckets and directories and other great Amazon S3 abstraction layers in the AWS SDK for PHP User Guide.