[英]Paperclip URL helper providing wrong URL when using S3
I am using the Paperclip gem with Rails to upload images, and when I use the img tag helper with the gem it outputs the wrong URL. 我将Paperclip gem与Rails一起使用来上传图像,当我将img标签帮助程序与gem一起使用时,它会输出错误的URL。 Here is the model code:
这是模型代码:
class Org < ActiveRecord::Base
has_many :event
has_many :solookup
belongs_to :student
has_attached_file :org_pic, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/assets/clubhouse.jpg", :storage => :s3, :s3_credentials => Proc.new{|a| a.instance.s3_credentials}, :s3_host_name => "branchapp.s3.amazonaws.com"
validates_attachment_content_type :org_pic, :content_type => /\Aimage\/.*\Z/
def s3_credentials
{:bucket => "branchapp", :access_key_id => "hidden", :secret_access_key => "hidden"}
end
end
The upload works great, but the outputted url is like so: 上传效果很好,但是输出的网址是这样的:
http://branchapp.s3.amazonaws.com/branchapp/orgs/org_pics/000/000/002/original/IMG_0539.JPG?1396413590 http://branchapp.s3.amazonaws.com/branchapp/orgs/org_pics/000/000/002/original/IMG_0539.JPG?1396413590
I cannot figure out how to remove the /branchapp after the .com. 我无法弄清楚如何在.com之后删除/ branchapp。 If that is removed the link works without problem.
如果将其删除,则链接可以正常工作。 How can I do this?
我怎样才能做到这一点?
In has_attached_file
you need to override the url
option. 在
has_attached_file
您需要覆盖url
选项。 By default the url uses ":s3_path_url"
which puts the bucket in the url like you see. 默认情况下,该URL使用
":s3_path_url"
,这将存储桶放入您所看到的URL中。 You need to use ":s3_domain_url"
instead. 您需要改用
":s3_domain_url"
。
Add: 加:
:url => ":s3_domain_url"
to your has_attached_file
options. 您的
has_attached_file
选项。
Note: ":s3_domain_url"
should prefix the host with the bucket name so you might need to remove branchapp
from your s3_host_name
option. 注意:
":s3_domain_url"
应在主机名称前添加存储桶名称,因此您可能需要从s3_host_name
选项中删除branchapp
。 ( :s3_host_name => "s3.amazonaws.com"
) (
:s3_host_name => "s3.amazonaws.com"
)
To give you some more input, you may wish to look at the :s3 documenation for Paperclip . 为了给您更多的输入,您可能希望看看Paperslip的:s3文档 。 We use this setup (which works great):
我们使用以下设置(效果很好):
#config/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
Paperclip::Attachment.default_options.merge!({
storage: :s3,
s3_host_name: 's3-eu-west-1.amazonaws.com',
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
},
bucket: ENV['S3_BUCKET_NAME']
})
This allows us to call: 这使我们可以调用:
#app/models/image.rb
Class Image < ActiveRecord::Base
has_attached_file :image
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.