| Did this page help you? Yes No Tell us about it... |
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 |
2 | Execute the If you are uploading a file, you specify the file
name by adding the array parameter with the
|
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);
}