Problems setting up Paperclip + AWS S3 for storing images in our Rails3 / Heroku app

We have already created the rails application, in which there are several users and an image for each of them. Carrying out the work of all our developers on our local host, we have working seeds for users and photos ... but now, when we try to use S3 to store images, we encounter errors during ... always during the "seed", migration step when you do this:

rake db: migrate: reset

Sorry for this question, but we’ve been breaking our head for 11 hours now, going through every Stack’s related question on this issue. Many similar messages had a NoSuchBucket error and other types of problems, but none of the proposed changes fixed our problem ... maybe this is due to the latest versions of the gems we use?

We use Rails 3.0.4, Ruby 1.8.7, Paperclip 2.3.8, aws-s3 0.6.2

We add seeds for the initial users and a photo for each user using our seeds.rb file in the / migrate / folder. This always worked when storing files and images on the local computer (using paperclip, but not S3). We also tested the removal of the seed file and simply created a new user with a working application and received the same error:

Credentials are not a path, file or hash

For the user module, we tested both the option where we installed the following S3 keys both through (a) the yml file and (b) directly in the user model. access_key_id: "secret" secret_access_key: 'secret'

We tried to do this from our local host (we still do not live on the hero), and we also tried to run it through Heroku.

We tried, it would seem, every permutation of the layout of these keys, but most often we get an error:

cannot convert module to hash

Enabling this error message returns zero results, so we don’t know what is going on there. This was the most unpleasant part ... it would seem that every attempt made us return to this error.

We also tried both:

(1) hard-coded access keys in the user model, and so:

:access_key_id => ENV['accesskeyid'], :secret_access_key => ENV['secretaccesskey'], 

In this case, we often get this error:

You did not provide both required passkeys. Please provide access_key_id and secret_access_key.

Disappointment, because we always had both of the items listed, checked with and without quotes, reordered, etc.

We tried both (a) with ENV ['accesskeyid'] and (b) without these ... with just blahblah => 'accesskeyid'.

and (2) entering the keys into the yml file, for example:

 has_attached_file :photo, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => "/:photo/:filename" 

with this in the yml file:

 development: access_key_id: accesskeyid secret_access_key: secretaccesskey bucket: ourbucketname production: access_key_id: accesskeyid secret_access_key: secretaccesskey bucket: ourbucketname 

We tried this with single quotes around keys and without.

We also tried to define the bucket in the model, not in the yml file, and we got the same error.

and (3) by setting it this way:

if Rails.env == "production" S3_CREDENTIALS = {: access_key_id => ENV ['S3_KEY'] ,: secret_access_key => ENV ['S3_SECRET'] ,: bucket => "ourbucket"} else S3_CREDENTIALS = Rails.root.join ("config / s3.yml")
end

has_attached_file: photo,: storage =>: s3,: styles => {: small => "50x50>",: thumb => "75x75>",: medium => "400x400>"},: path => "/ : photo /: filename "

With the same content in our yml file.

This gave us this error:

credentials are not a file, outline or hash

Naturally, we checked four times that we had the correct access keys (from our AWS account) and tested several different ways to configure the hash, but did not get what we wanted.

Here is the relevant part of the Gemfile: gem 'aws-s3' ,: require => 'aws / s3' # For storing images on Amazon gem 'paperclip'

As another attempt, we tried using gem right_aws instead of the Gemfile, but this led to this error:

there is no such file to download - aws / s3 (you may need to install the aws-s3 gem)

Please note: we did all this and hit all these errors by migrating from the local host and not from the Heroku live application, but we couldn’t even go through this simple step of “seed users”.

Our bucket is currently called media.oururl.com. Is there a problem with having periods in the bucket name?

Going to ask the hero about this, guys, but given how amazing this community is, I hope one of you knows what we are doing wrong with it.

MUCH appreciated - and I hope this helps others who follow us.

+4
source share
1 answer

great question. Some time ago I spent some time with a similar problem.

The main problem is that you need to move the following code to your own initialization file:

 if Rails.env == "production" S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "ourbucket"} else S3_CREDENTIALS = Rails.root.join("config/s3.yml") end 

Then you should add the following line to your model, where you have * has_attached_file: photo * The line to add.

 :s3_credentials => S3_CREDENTIALS, 

This is what you were missing before.

In addition, if you declare your bucket name, make sure that this is our standard. If you use one of the other places, you need to update the path accordingly.

Hope this helps!

+5
source

Source: https://habr.com/ru/post/1344721/


All Articles