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 an Object Using the AWS SDK for PHP

The following tasks guide you through PHP classes to upload an object of up to 5 GB in size. For larger files you must use multipart upload API. For more information, see Uploading Objects Using Multipart Upload API.

Uploading Objects

1

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

2

Execute the AmazonS3::create_object() method. You need to provide information such as a bucket name, and key name.

If you are uploading a file, you specify the file name by adding the array parameter with the fileUpload key. You can also provide the optional object metadata using the array parameter.


The following PHP code sample demonstrates the preceding tasks. The code sample creates an object by uploading a file specified in the fileUpload key in the array parameter.

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

$response = $s3->create_object(
    $bucket,
    $keyname2,
    array(
       'fileUpload'  => $filePath,
       'acl'         => AmazonS3::ACL_PUBLIC,
       'contentType' => 'text/plain',
       'storage'     => AmazonS3::STORAGE_REDUCED,
       'headers'     => array( // raw headers
                          'Cache-Control'    => 'max-age',
                          'Content-Encoding' => 'gzip',
                          'Content-Language' => 'en-US',
                          'Expires'       => 'Thu, 01 Dec 1994 16:00:00 GMT',
          ),
         'meta'     => array(
                         'param1' => 'value 1',
                         'param2' => 'value 2'
         ) )
    );
print_r($response);

Instead of specifying a file name, you can provide object data inline by specifying the array parameter with the body key, as shown in the following PHP code example.

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

$response = $s3->create_object(
                           $bucket,
                           $keyname1,
                           array('body' => 'Sample object data')
);

Example

The following PHP example creates two objects in a specified bucket. The example creates an object first by uploading a file and then another object using the text string provided in the code. The example illustrates the use of the create_object() method to upload a file.

<?php
require_once '../aws-sdk-for-php/sdk.class.php';

$bucket   = '*** Provide bucket name ***';
$keyname1 = '*** Provide object key ***';
$filepath = '*** Provide file name to upload ***';

$keyname2 = '*** Provide another object key ***';

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

// 1. Create object from a file.
$response = $s3->create_object(
    $bucket,
    $keyname1,
    array('fileUpload' => $filepath)
    );

// Success?
print_r($response->isOK());
if ($response->isOK())
{
    echo "First upload successful!";
}

// 2. Create object from a string.
$response = $s3->create_object(
    $bucket,
    $keyname2,
    array(
       'body'        => 'This is object content',
       'acl'         => AmazonS3::ACL_PUBLIC,
       'contentType' => 'text/plain',
       'storage'     => AmazonS3::STORAGE_REDUCED,
       'headers'     => array( // raw headers
                          'Cache-Control' => 'max-age',
                          'Content-Encoding' => 'gzip',
                          'Content-Language' => 'en-US',
                          'Expires' => 'Thu, 01 Dec 2010 16:00:00 GMT',
          ),
    	  'meta'     => array(
                          'param1' => 'value 1',
                          'param2' => 'value 2'
          )
    )
    );

// Success?
print_r($response->isOK());
if ($response->isOK())
{
    echo "Second upload successful!";
}
else
{
    print_r($response);
}