简体   繁体   中英

Fog directory appends local system path with amazon url, when hosting images to s3 with paperclip gem

I am trying to host my website that have paperclip attachment images on aws-s3 with fog gem. But my fog directory takes the wrong path but it appends my local file system path with it.

this is my code

class RealEstate < ActiveRecord::Base
  has_attached_file :image,
                    :storage => :fog,
                    :fog_credentials => "#{Rails.root}/config/s3.yml",
                    :fog_directory => "#{Rails.root}/config/fog.yml"
end

if i define the bucket name here only then it would work but then it would not be able to use different bucket for different env

:fog_directory => "development_bucket_name" #works fine but cant use different bucket for different env

this is my fog.yml

development:
  fog_directory: development_bucket
staging:
  fog_directory: testing_bucket
production:
  fog_directory: production_bucket

the path it creates is:

https://s3.amazonaws.com//home/Desktop//config/fog.yml/real_estate/image/000/000/185/original/4bec7.png?1396429186

Paperclip has no idea that the string you're passing is a path to a config file - it's expecting the actual bucket name.

You need to parse the yaml file and extract the bucket name from it. For example

directories = YAML.load(File.read(Rails.root.join('config', 'fog.yml')))
has_attached_file :image,
                  :storage => :fog,
                  :fog_credentials => "#{Rails.root}/config/s3.yml",
                  :fog_directory => directories[Rails.env]['fog_directory']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM