PHP removes remote image on Amazon S3

Is there a way to capture an image using a URL and save it directly to Amazon?

Using PHP?

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

+4
source share
3 answers

When you β€œcapture an image”, you will have to at least write it to a temporary file locally. This is because if you use fopen () or curl to access the file, you will need some way to write the stream to Amazon. I am not sure about the possibility of programming a stream that essentially connects the remote file directly to S3. In fact, this is theoretically impossible, since S3 cannot run scripts, and the image cannot run a script.

You can load an image through a buffer of some form of buffer if you want to minimize the amount of information stored in memory, but writing it to a temporary file should be the easiest. If you do not have enough space because you have so many users in the system, you either go to a large server or add another server under the load balancer. Personally, I use the Amazon S3 PHP class on my system and move it from the temporary file locally directly to S3 using a script like this:

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; } 

It is clear that this is not a reliable script, but decided that I would just talk about the path that I myself took down. I have to add, since it seems that your main concern in this case is computing power, look at this interesting post when resizing images in the cloud. http://www.nslms.com/2010/11/01/how-to-resize-billions-of-images-in-the-cloud/

Update In accordance with this answer How to resize images outside the server "PHP / GD allows you to send jpeg directly to the HTTP response".

+5
source

You can save deleted files directly to S3 using Amazon S3 Stream Wrapper . There is no need to save a temporary 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 the built-in PHP functions such as file_get_contents , fopen , copy , rename , unlink , mkdir , rmdir , etc.

+1
source

Of course, you can upload the image to your server and then transfer it to Amazon S3. But, I think it would be better to enable the client to upload the image directly to S3 using the 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 the official documentation: http://aws.amazon.com/articles/1434

0
source

Source: https://habr.com/ru/post/1379390/


All Articles