Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saadaan's avatar

Uploading API URL file to AWS S3

Hi,

I am receiving some data through an API, which gives me the URL for an image as well. For downloading the image and storing locally, I use the following code:

$car_photo = $request->data[0]['car_photo']['url'];
$imgname = '1.jpg';
file_put_contents('img/carimages/'.$imgname, file_get_contents($car_photo));

This code picks up the URL from API, pulls its content, and saves it locally to a folder.

Now, instead of storing it locally, I want to upload it to AWS s3 bucket. Previously, I was using following code to upload an incoming file through browser upload and put it on s3 bucket successfully:

$request->file('car_photo')->storePubliclyAs('img/carimgs', $imagename, 's3');

But this code assumes that the REQUEST is carrying the file object; while in the new scenario, $request is only carrying a URL and I am pulling the file locally. Hence I cannot use file methods like 'storePubliclyAs', etc.

How can I send the local file to s3?

Thanks, Saad

0 likes
2 replies
theUnforgiven's avatar

You could try something like:

Storage::disk('s3')->put('img/carimages/' . $imgname, $image, 'public');

This would store on S3 if you have everything set within your env file.

martinbean's avatar

@saadaan Use Guzzle to stream the contents to S3. You don’t want to pull the image bytes to your server, just to move them to S3. Just send them directly to S3.

Please or to participate in this conversation.