Carrierwave fog Amazon S3 images not displaying
Asked Answered
D

1

20

I have installed carrierwave and fog, have successfully uploaded the images and viewed them the first time, but now it does not show the images anymore.

Here is my config file app/config/initializers/carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                                       # required
    :aws_access_key_id      => 'AKIAJKOHTE4WTXCCXAMA',                      # required
    :aws_secret_access_key  => 'some secret key here',                      # required
    :region                 => 'eu-east-1',                                 # optional, defaults to 'us-east-1'
    :host                   => 'https://s3.amazonaws.com',                  # optional, defaults to nil
    :endpoint               => 'https://s3.amazonaws.com:8080'              # optional, defaults to nil
  }
  config.fog_directory  = 'createmysite.co.za'                    # required
  config.fog_public     = false                                   # optional, defaults to true
  #config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end

This is what the url looks like of the image that is supposed to display

<img alt="Normal_selection_003" src="https://createmysite.co.za.s3.amazonaws.com/uploads/portfolio/image/3/normal_Selection_003.png?AWSAccessKeyId=AKIAJKOHTE4WTXCCXAMA&amp;Signature=8PLq8WCkfrkthmfVGfXX9K6s5fc%3D&amp;Expires=1354859553">

when I open the image url this is the output from amazon https://createmysite.co.za.s3.amazonaws.com/uploads/portfolio/image/3/normal_Selection_003.png?AWSAccessKeyId=AKIAJKOHTE4WTXCCXAMA&Signature=8PLq8WCkfrkthmfVGfXX9K6s5fc%3D&Expires=1354859553

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>3F179B7CE417BC12</RequestId>
<HostId>
zgh46a+G7UDdpIHEEIT0C/rmijShOKAzhPSbLpEeVgUre1iDc9f7TSOwaJdQpR65
</HostId>
</Error>

Update

new config file (added fog url expiry) app/config/initializers/carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                                       # required
    :aws_access_key_id      => 'AKIAJKOHTE4WTXCCXAMA',                      # required
    :aws_secret_access_key  => 'chuck norris',  # required
  }
  config.fog_directory  = 'createmysite.co.za'                              # required
  config.fog_public     = false                                             # optional, defaults to true
  config.fog_authenticated_url_expiration = 600                             # (in seconds) => 10 minutes
end

works like a charm!

Discernment answered 7/12, 2012 at 5:54 Comment(0)
A
29

You've set config.fog_public to false and are using Amazon S3 for storage. URLs for private files through S3 are temporary (they're signed and have an expiry). Specifically, the URL posted in your question has an Expires=1354859553 parameter.

1354859553 is Fri, 07 Dec 2012 05:52:33 GMT, which is in the past from the current time, so the link has effectively expired, which is why you're getting the Access Denied error.

You can adjust the expiry out further (the default is 600 seconds) by setting

config.fog_authenticated_url_expiration = ... # some integer here

If you want non-expiring links either

  • set config.fog_public to true
  • have your application act as a middle man, serving the files up through send_file. Here is at least one question on SO covering this
Acierate answered 7/12, 2012 at 6:8 Comment(5)
hi, this sounds correct! I must have something buggering it up lol!, Ill post updateDiscernment
haha, my dev and production have diffirent db's so images where not showing on local, pulled prod db and now it seems to work pretty nicely. TX!Discernment
If I change fog_public from false to true when I have images uploaded and live on my app, how do I get the images working correctly? Reprocess them?Casuistry
You need to change the ACL on those objects to allow 'everyone' view permissions. One way is to write a small script (or even a rake task through your Rails app) to iterate the objects in the bucket, marking each to be public. Check out acl= in the fog gem. You would just set this to "public-read" for each file and call save. There may be a simpler way directly through the AWS panel, but you can see how simple it is with this gem.Acierate
@Acierate can you please tell me the maximum expiry time of fog_authenticated_url_expirationBessette

© 2022 - 2024 — McMap. All rights reserved.