Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Upload a File

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

High-Level API File Uploading Process

1

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

2

Execute the AmazonS3::create_mpu_object() method.


The following PHP code sample demonstrates the preceding tasks.

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

$response = $s3->create_mpu_object($bucket, $keyname, array(
    'fileUpload' => $filepath
));

print_r($response);

Example

The following PHP example uploads a file to an Amazon S3 bucket. The example illustrates the use of the AmazonS3::create_mpu_object() method to upload a file. Each successive call to upload replaces the previous upload.

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

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

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

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

// 1. Upload file. Specify object key name explicitly.
$response = $s3->create_mpu_object($bucket, $keyname, array(
    'fileUpload' => $filepath
));

// Success?
header('Content-Type: text/plain; charset=utf-8');
print_r($response);
echo "Upload 1 completed!" . PHP_EOL . PHP_EOL;

// 2. Specify optional configuration.
$response = $s3->create_mpu_object($bucket, $keyname, array(
    'fileUpload' => $filepath,

    // Optional configuration
    'partSize' => 40*MB, // Defaults to 50MB
    'acl' => AmazonS3::ACL_PUBLIC,
    'storage' => AmazonS3::STORAGE_REDUCED,

    // Object metadata.
    'meta' => array(
        'param1' => 'value1',
        'param2' => 'value2',
     )
));

// Success?
print_r($response);
echo "Upload 2 completed!";