Is there a way to grab an image using URL and save it directly to Amazon?
Can it be done using PHP?
My other option was to save file locally and send it using the S3 PHP class.
Is there a way to grab an image using URL and save it directly to Amazon?
Can it be done using PHP?
My other option was to save file locally and send it using the S3 PHP class.
When you "grab the image", you are going to have to at a minimum write it to a temporary file locally. That's because whether you use fopen() or curl to access the file you are going to need some way to write the stream to Amazon. I'm not sure of a way to program a stream that essentially connects the remote file directly to S3. In fact, it's theoretically impossible as S3 cannot execute scripts and the image cant run a script.
You could load the image through some form of stream buffer if you are looking to minimize the amount of information stored in memory, but writing it to a temporary file should be the easiest thing. If you run out of space because you have so many users in the system you either upgrade to a large server or add another server under a load balancer. Personally, I use Amazon's S3 PHP class on my system and move it from a temp file locally directly to S3 using a script like this one:
function upload_image( $image_data )
{
//
// Write the image to S3
//
$s3 = new AmazonS3();
$bucket = ACTIVITY_S3_BUCKET;
$image_id = uniqid('myscript');
$path = ACTIVITY_S3_FOLDER.'/'.$image_id;
$response = $s3->create_object($bucket, $path, array(
'body' => $image_data,
'acl' => AmazonS3::ACL_PUBLIC
));
$image_url = 'https://s3.amazonaws.com/'.ACTIVITY_S3_BUCKET.'/'.$path;
return $image_id;
}
Clearly this isn't a robust script but figured I'd just share the path that I've gone down myself. I should add, as it seems your primary concern in this case would be processing power, check out this interesting post on resizing billions of images in the cloud. http://www.nslms.com/2010/11/01/how-to-resize-billions-of-images-in-the-cloud/
Update According to this answer How best to resize images off-server "PHP/GD lets you send jpeg directly in http response."
You can save remote files directly to S3 with the Amazon S3 Stream Wrapper. No need to save a temp copy on your server.
// Register the stream wrapper from an S3Client object
$client->registerStreamWrapper();
// Copy a remote file to Amazon S3
copy('http://example.com/1.jpg', 's3://bucket/key');
It also allows you to store and retrieve data from Amazon S3 using built-in PHP functions like file_get_contents
, fopen
, copy
, rename
, unlink
, mkdir
, rmdir
, etc
Of course, you can upload image to your server and then transfer it to Amazon S3 service. But, I think, it will be better to give ability for client to upload image directly to S3 using form:
<form action="https://s3-bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
<input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
<input type="hidden" name="Content-Type" value="image/jpeg">
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
You can find detailed information in official documentation: http://aws.amazon.com/articles/1434