Reputation: 932
I've update my Rails app to store uploaded images in an AWS bucket when in production mode. It's working fine and I can see the image getting uploaded to AWS when using (Heroku). One thing however, When I update or create a new course the image uploaded becomes the image across all courses in the application. How do I distinguish the view to locate the particular image for each course? When I see the url for each image there is a different identifier at the end of the url for each image. So it is not the same image but looks like it's just being copied across all images in AWS or something. Any pointers in the right direction will be much appreciated. Thanks lads.
production.rb
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY')
},
s3_host_name: "s3-eu-west-1.amazonaws.com", # Added entry
url: ":s3_host_name",
s3_region: 'eu-west-1' # Added entry
}
Not sure what other code is relevant here in my application. I'll add as requested. Thanks.
Upvotes: 0
Views: 31
Reputation: 932
Add to config/production.rb file
# config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3-eu-west-1.amazonaws.com",
url: ':s3_domain_url',
path: "/:class/:attachment/:id_partition/:style/:filename",
s3_region: 'eu-west-1',
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY')
}
}
So notice the changes I've made. I now call the url: ':s3_domain_url' and match that with the path: key. This allows each image to be stored uniquely. Whereas previously it was saving to s3_host_name and this was saving the image over the last saved and nothing being unique.
Anyways simple enough to sort once I understood it a bit better. Hope this helps somebody down the line.
Upvotes: 0