| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
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 |
2 | Initiate multipart upload by executing the
|
3 | Retrieve the |
4 | Call the |
5 | Upload parts by executing the |
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!';
}