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

Upload a File in Multiple Parts Using the PHP SDK Low-Level API

This topic guides you through using low-level multipart upload classes from the AWS SDK for PHP to upload a file in multiple parts.

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.

PHP SDK Low-Level API Multipart File Upload Process

1

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

2

Initiate multipart upload by executing the Aws\S3\S3Client::createMultipartUpload() method. You must provide a bucket name and a key name in the array parameter's required keys, Bucket and Key.

Retrieve and save the UploadID from the response body. The UploadID is used in each subsequent multipart upload operation.

3

Upload the file in parts by executing the Aws\S3\S3Client::uploadPart() method for each file part until the end of the file is reached. The required array parameter keys for upload_part() are Bucket, Key, UploadId, and PartNumber. You must increment the value passed as the argument for the PartNumber key for each subsequent call to upload_part() to upload each successive file part.

Save the response of each of the upload_part() methods calls in an array. Each response includes the ETag value you will later need to complete the multipart upload.

4

Execute the Aws\S3\S3Client::completeMultipartUpload() method to complete the multipart upload. The required array parameters for completeMultipartUpload() are Bucket, Key, and UploadId.

The following PHP code example demonstrates uploading a file in multiple parts using the PHP SDK low-level API.

Copy
use Aws\S3\S3Client; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $filename = '*** Path to and Name of the File to Upload ***'; // 1. Instantiate the client. $s3 = S3Client::factory(); // 2. Create a new multipart upload and get the upload ID. $response = $s3->createMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname )); $uploadId = $response['UploadId']; // 3. Upload the file in parts. $file = fopen($filename, 'r'); $parts = array(); $partNumber = 1; while (!feof($file)) { $result = $s3->uploadPart(array( 'Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'PartNumber' => $partNumber, 'Body' => fread($file, 5 * 1024 * 1024), )); $parts[] = array( 'PartNumber' => $partNumber++, 'ETag' => $result['ETag'], ); } // 4. Complete multipart upload. $result = $s3->completeMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'Parts' => $parts, )); $url = $result['Location']; fclose($file);

Example of Uploading a File to an Amazon S3 Bucket Using the Low-level Multipart Upload PHP SDK API

The following PHP code example uploads a file to an Amazon S3 bucket using the low-level PHP API multipart upload. For information about running the PHP examples in this guide, go to Running PHP Examples.

Copy
<?php // Include the AWS SDK using the Composer autoloader require 'vendor/autoload.php'; use Aws\S3\S3Client; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $filename = '*** Path to and Name of the File to Upload ***'; // 1. Instantiate the client. $s3 = S3Client::factory(); // 2. Create a new multipart upload and get the upload ID. $result = $s3->createMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'StorageClass' => 'REDUCED_REDUNDANCY', 'ACL' => 'public-read', 'Metadata' => array( 'param1' => 'value 1', 'param2' => 'value 2', 'param3' => 'value 3' ) )); $uploadId = $result['UploadId']; // 3. Upload the file in parts. try { $file = fopen($filename, 'r'); $parts = array(); $partNumber = 1; while (!feof($file)) { $result = $s3->uploadPart(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId, 'PartNumber' => $partNumber, 'Body' => fread($file, 5 * 1024 * 1024), )); $parts[] = array( 'PartNumber' => $partNumber++, 'ETag' => $result['ETag'], ); echo "Uploading part {$partNumber} of {$filename}.\n"; } fclose($file); } catch (S3Exception $e) { $result = $s3->abortMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId )); echo "Upload of {$filename} failed.\n"; } // 4. Complete multipart upload. $result = $s3->completeMultipartUpload(array( 'Bucket' => $bucket, 'Key' => $keyname, 'UploadId' => $uploadId, 'Parts' => $parts, )); $url = $result['Location']; echo "Uploaded {$filename} to {$url}.\n";

Related Resources