I have been trying for several weeks to correctly format a REST request in the Amazon AWS S3 API using available examples on the web
Have you tried the Amazon AWS SDK for PHP ? It is comprehensive, complete, and most importantly, written by Amazon. If their own code does not work for you, something will be really wrong.
Here is sample code using the linked SDK to load example.txt in the current directory into a bucket named 'my_very_first_bucket'.
<?php // Complain wildly. ini_set('display_errors', true); error_reporting(-1); // Set these yourself. define('AWS_KEY', ''); define('AWS_SECRET_KEY', ''); // We'll assume that the SDK is in our current directory include_once 'sdk-1.3.1/sdk.class.php'; include_once 'sdk-1.3.1/services/s3.class.php'; // Set the bucket and name of the file we're sending. // It happens that we're actually uploading the file and // keeping the name, so we're re-using the variable // below. $bucket_name = 'my_very_first_bucket'; $file_to_upload = 'example.txt'; // Fire up the object $s3 = new AmazonS3(AWS_KEY, AWS_SECRET_KEY); // This returns a "CFResponse" $r = $s3->create_object( $bucket_name, $file_to_upload, array( // Filename of the thing we're uploading 'fileUpload' => (__DIR__ . '/' . $file_to_upload), // ACL'd public. 'acl' => AmazonS3::ACL_PUBLIC, // No wai. 'contentType' => 'text/plain', // The docs say it'll guess this, but may as well. 'length' => filesize(__DIR__ . '/' . $file_to_upload) ) ); // Did it work? echo "Worked: "; var_dump($r->isOK()); // Status as in HTTP. echo "\nStatus: "; var_dump($r->status); // The public URL by which we can reach this object. echo "\nURL: "; echo $s3->get_object_url($bucket_name, $file_to_upload); // Tada! echo "\n";
Relevant API docs:
You can navigate through the rest of the methods in the menu on the left. It is quite comprehensive, including creating a new bucket, managing, deleting, the same for objects, etc.
You should be able to basically abandon this code and whether to work correctly. PHP 5.2-safe.
Edit Silver Tiger:
Charles -
The method you provide uses the SDK API functions to load a file from the local file system into the bucket of my choice. I have this part already working through Flex, and the download works like a charm. This problem is to send an AWS S3 REST request to change the file name from its current βloadedβ name to a new name, a more suitable name that will work with my end (database, tracking, etc., Which I process and display separately in PHP using MyySQL).
AWS S3 does not really support the βcopyβ function, so they provided a method for re-βPUTβ the file by reading the source from your own bucket and placing a new copy using a different name in the same bucket. The difficulty I ran into is handling the REST request, hence the HMAC encryption.
I really appreciate your time and understand the example that you provided, since I also have a working copy of the PHP download, which functioned before I developed the Flex application. The reason for Flex was the inclusion of status updates and a dynamically updated progress bar, which also works like a charm :).
I will continue to offer the REST solution from the point of view of Amason zupport, this will be the only way that I can rename the file that already exists in my bucket for each support team.
As always, if you have any input or suggestions regarding submitting a REST, I would appreciate any feedback.
Thanks,
Silver tiger
Copy / delete proof works:
$r = $s3->copy_object( array( 'bucket' => $bucket_name, 'filename' => $file_to_upload ), array( 'bucket' => $bucket_name, 'filename' => 'foo.txt' ) ); // Did it work? echo "Worked: "; var_dump($r->isOK()); // Status as in HTTP. echo "\nStatus: "; var_dump($r->status); // The public URL by which we can reach this object. echo "\nURL: "; echo $s3->get_object_url($bucket_name, 'foo.txt'); echo "\nDelete: "; // Nuke? $r = $s3->delete_object($bucket_name, $file_to_upload); // Did it work? echo "Worked: "; var_dump($r->isOK()); // Status as in HTTP. echo "\nStatus: "; var_dump($r->status);
Edit Silver Tiger:
Charles -
No need for REST, no worries ... SDK 1.3.1 and your help solves the problem. The code I used for testing is very similar to yours:
// Complain wildly. ini_set('display_errors', true); error_reporting(-1); // Set these yourself. define('AWS_KEY', 'removed for security'); define('AWS_SECRET_KEY', 'removed for security'); // We'll assume that the SDK is in our current directory include_once 'includes/sdk-1.3.1/sdk.class.php'; include_once 'includes/sdk-1.3.1/services/s3.class.php'; // Set the bucket and name of the file we're sending. // It happens that we're actually uploading the file and // keeping the name, so we're re-using the variable // below. $bucket = 'bucket'; $file_to_upload = 'example.txt'; $Source_file_to_copy = 'Album.jpg'; $Destination_file = 'Album2.jpg'; // Fire up the object // Instantiate the class $s3 = new AmazonS3(); $response = $s3->copy_object( array( // Source 'bucket' => $bucket, 'filename' => $Source_file_to_copy ), array( // Destination 'bucket' => $bucket, 'filename' => $Destination_file ) ); // Success? var_dump($response->isOK());
Now I will perform the deletion after the copy, and we are gold. Thank you sir for your understanding and help.
Silver tiger