Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP save remote image to Amazon S3

is there a way to grab an image using URL and save it directly to Amazon?

Using PHP?

My other option was to save file locally and send it using the S3 PHP class.

like image 276
Scott Yu - builds stuff Avatar asked Nov 02 '11 22:11

Scott Yu - builds stuff


People also ask

How do I upload images to Amazon S3?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

Is S3 good for image hosting?

Amazon S3 offers scalable storage that lets businesses store and manage endless amounts of items in the cloud. It not only offers huge storage allowance but it does so with extremely fast speeds, allowing users to quickly retrieve and share their stored images.


2 Answers

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."

like image 193
Nick ONeill Avatar answered Sep 21 '22 01:09

Nick ONeill


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

like image 38
Bxx Avatar answered Sep 20 '22 01:09

Bxx