Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return Cloudfront url after S3 upload?

I'm uploading a file to an S3 bucket for which I've created a Cloudfront distribution. I'm using the Aws\S3\S3Client class.

After uploading with putObject, the response object and the getObjectUrl method both return the object's url as https://s3-eu-west-1.amazonaws.com/mybucket/path/myfile.jpg. I am trying to get the Cloudfront url which would be something like https://d111111111ck.cloudfront.net/path/myfile.jpg.

Is there any way to get this url directly, or do I have to build it from my distribution hostname and file path?

like image 426
juuga Avatar asked Oct 20 '25 14:10

juuga


2 Answers

You won't get a cloudfront URL from S3, it's a different service. If your using putObject then you already know the file path (value specified in Key).

Just return the cloudfront URL in front of the file path e.g...

$filePath = '/path/file.jpg';

$client->putObject(array(
    'Bucket' => 'mybucket',
    'Key' => $filePath,
    'SourceFile' => $fileSource,
    'ACL' => 'public-read'
));

return 'https://d111111111ck.cloudfront.net' . $filePath;
like image 59
fire Avatar answered Oct 23 '25 05:10

fire


I guess there's is no way to resolve an associated cloudfront url for the bucket. Yes, It is!

CloudFront is a CDN caching. You did not need to get the URL to s3 bucket/file because CloudFront cares about this for you.

More details about AWS CloudFront here.

Follow the steps and see the step 24. That's it!

Screenshots below:

enter image description here enter image description here

Enjoy! :)

like image 25
Francis Rodrigues Avatar answered Oct 23 '25 03:10

Francis Rodrigues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!