Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Amazon S3 files: get full url of file uploaded in json response via postman

LOUY ABDALRIDA asked in General

Hello I have api rails app that I can upload files via carrierwave to my s3 bucket and I got back checksum and id in the response now I am trying to add the url of the file uploaded with the json response ?
now I have this :
{
"id": 5,
"checksum": "4B1E2AAFE1BC3B7D79AB39F2834FEF81826054610DB951AA60D3965F609BB6",
"file_refrence": null
}

Reply

What is the name of the carrierwave string column you use for this file?

If it file for example than the url is simply file_url as a result of the fact that you are using carrierwave. Or if you wanted a specific version, you could use file.thumb.url.

Reply

Thank you Tomas Bush
That will return the file path not the uploaded url file!
I am looking for the AWS s3 url

Reply

So I may be making inacurate assumptions, but I think you want to receive a cloudfront url instead of an s3 url. Cloudfront is a CDN distribution that you attach to an s3 bucket. If so, I layout the process below. Once done, restart your app and file_url should return a cloudfront url that is the image in the s3 bucket.

Reason you would want cloudfront url instead of s3 url
s3 is intentionally not overly quick retrieving/serving assets -- you will want to create a cloudfront distribution and attach it to the s3. I believe its a pretty straight forward process, just essentially choose your s3 bucket in the cloudfront creation process -- cloudfront will keep itself in sync with your bucket so no worries there. In that scenario, s3 accepts and stores images from your rails app, while cloudfront is the CDN for your s3 bucket -- so all images will have a cloudfront base url.

Than add one more line to your config/initializers/carrierwave.rb, it will look like this:
config.asset_host = 'https://your-cloudfront-base.cloudfront.net'

Example carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    ...your credentials
  }
  config.fog_directory  = 'YOUR-BUCKET'
  config.asset_host = 'https://your-cloudfront-base.cloudfront.net'
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end
Reply

Thank you Tomas Bush
sorry been busy thank you so much for you help ;D

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.