Upload external file to AWS S3 bucket using PHP SDK

I want to upload a file from an external URL directly to an Amazon S3 bucket using the PHP SDK. I managed to do this with the following code:

$s3 = new AmazonS3(); $response = $s3->create_object($bucket, $destination, array( 'fileUpload' => $source, 'length' => remote_filesize($source), 'contentType' => 'image/jpeg' )); 

If the remote_filesize function is as follows:

 function remote_filesize($url) { ob_start(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); $ok = curl_exec($ch); curl_close($ch); $head = ob_get_contents(); ob_end_clean(); $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $head, $matches); return isset($matches[1]) ? $matches[1] : "unknown"; } 

However, it would be nice if I could skip the file size setting when uploading to Amazon, as this would save me from traveling to my own server. But if I remove the setting of the length property in the $ s3-> create_object function, I get the error message "Stream size for streaming download cannot be determined." Any ideas how to solve this problem?

+6
source share
2 answers

You can upload the url file directly to Amazon S3, like this (my example is about a jpg image):

1. Convert content from URL to binary

 $binary = file_get_contents('http://the_url_of_my_image.....'); 

2. Create an S3 object with a body to pass the binary code to

 $s3 = new AmazonS3(); $response = $s3->create_object($bucket, $filename, array( 'body' => $binary, // put the binary in the body 'contentType' => 'image/jpeg' )); 

This is all very fast. Enjoy it!

+2
source

Do you have control over the remote server / host ?. If so, you can configure the php server to query the file locally and send data to you.

If not, you can use something like curl to check the header like this:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://sstatic.net/so/img/logo.png'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); var_dump($size); 

Thus, you are using a HEAD request and not downloading the entire file. However, you rely on a remote server, send the correct Content-length header.

0
source

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


All Articles