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...

Upload a File

The following tasks guide you through using the low-level PHP classes to upload a file.

Low-Level API File UploadingProcess

1

Create an instance of the AmazonS3 class, by providing your AWS credentials.

2

Initiate multipart upload by executing the AmazonS3::initiate_multipart_upload() method. You need to provide the required information, i.e., bucket name and object key.

3

Retrieve the UploadID from the response body. You will need to provide this upload ID for each subsequent multipart upload operation.

4

Call the AmazonS3::get_multipart_counts() to generate parts information. You need to provide the input file and the desired part size. The method returns an associative array of all the parts information, such as the part length and its starting position in the file.

5

Upload parts by executing the AmazonS3::upload_part() method. Save the response of each of the upload_part() methods in an array. Each response includes the ETag value you will later need to complete the multipart upload.

6

Execute the AmazonS3::complete_multipart_upload() method to complete the multipart upload.


The following PHP code sample demonstrates the preceding tasks.

// Instantiate the class.
$s3 = new AmazonS3();

// Define a megabyte
define('MB', 1048576);

// 1. Initiate a new multipart upload.
$response = $s3->initiate_multipart_upload($bucket, $keyname);

// Get the Upload ID.
$upload_id = (string) $response->body->UploadId;

// 2. Upload parts.
// Get part list for a given input file and given part size.
// Returns an associative array.
$parts = $s3->get_multipart_counts(filesize($filepath), 5*MB);

$responses = new CFArray(array());

foreach ($parts as $i => $part)
{
    // Upload part and save response in an array.
    $responses[] = $s3->upload_part($bucket, $keyname, $upload_id, array(
        'fileUpload' => $filepath,
        'partNumber' => ($i + 1),
        'seekTo' => (integer) $part['seekTo'],
        'length' => (integer) $part['length'],
    ));
}

// 3. Complete multipart upload. We need all part numbers and ETag values.
$parts = $s3->list_parts($bucket, $keyname, $upload_id);

$response = $s3->complete_multipart_upload(
    $bucket, $keyname, $upload_id, $parts);

Example

The following PHP code example uploads a file to an Amazon S3 bucket.

<?php
require_once '/path/to/sdk.class.php';

$bucket = '*** Provide your existing bucket name ***';
$keyname = '*** Provide object key name ***';
$filepath = '*** Provide file name to upload ***';

// Define a megabyte.
define('MB', 1048576);

// Instantiate the class
$s3 = new AmazonS3();

// 1. Initiate a new multipart upload. (Array parameter is optional)
$response = $s3->initiate_multipart_upload($bucket, $keyname, array(
    'acl' => AmazonS3::ACL_PUBLIC,
    'storage' => AmazonS3::STORAGE_REDUCED,
    'meta' => array(
        'param1' => 'value 1',
        'param2' => 'value 2',
        'param3' => 'value 3'
    )
));

if (!$response->isOK())
{
    throw new S3_Exception('Bad!');
}

// Get the Upload ID.
$upload_id = (string) $response->body->UploadId;

// 2. Upload parts.
// Get part list for a given input file and given part size.
// Returns an associative array.
$parts = $s3->get_multipart_counts(filesize($filepath), 5*MB);

$responses = new CFArray(array());

foreach ($parts as $i => $part)
{
    // Upload part and save response in an array.
    $responses[] = $s3->upload_part($bucket, $keyname, $upload_id, array(
        'fileUpload' => $filepath,
        'partNumber' => ($i + 1),
        'seekTo' => (integer) $part['seekTo'],
        'length' => (integer) $part['length'],
    ));
}

// Verify that no part failed to upload, otherwise abort.
if (!$responses->areOK())
{
    // Abort an in-progress multipart upload
    $response = $s3->abort_multipart_upload($bucket, $keyname, $upload_id);

    throw new S3_Exception('Failed!');
}

// 3. Complete the multipart upload. We need all part numbers and ETag values.
$parts = $s3->list_parts($bucket, $keyname, $upload_id);
$response = $s3->complete_multipart_upload(
                             $bucket, $keyname, $upload_id, $parts);

// Display the results
header('Content-Type: text/plain; charset=utf-8');
print_r($response);

if ($response->isOK())
{
    echo 'Object uploaded!';
}