Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave fog Amazon S3 images not displaying

Tags:

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!

like image 642
Francois Avatar asked Dec 07 '12 05:12

Francois


1 Answers

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
like image 183
deefour Avatar answered Oct 09 '22 08:10

deefour