Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

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! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top