我有一个Photo
模型,我正在运行paperclip (>= 3.4.0)
,ruby 2.3.7,Rails 3.2。
在我的photo.rb
中,我有以下内容:
class Photo < ActiveRecord::Base
attr_accessible :position, :source_uri, :img, :published
belongs_to :property
has_attached_file :img,
:styles => { :large => "590x380>", :medium => "380x200>", :small => "280x150>", :thumb => "100x75>" },
:url => "#{:source_uri}"
def source_uri=(uri)
self.img_file_name = "#{random_base_name}#{ext_from_mime}"
super
end
def source_uri
URI.unescape(super)
end
在我的config/environments/production.rb
中,我有这个:
config.paperclip_defaults = {
:storage => :s3,
:bucket => ENV['MY_S3_BUCKET'],
:path => "uploads/myapp/:class/:attachment/:id/:style/:filename",
:s3_credentials => "#{Rails.root}/config/aws.yml",
:default_url => ":attachment/:class/:style/missing.png"
}
在我的aws.yml
中,我有以下内容:
development:
access_key_id:
secret_access_key:
bucket:
staging:
access_key_id: <%= ENV["S3_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["S3_SECRET_ACCESS_KEY"] %>
bucket: <%= ENV["S3_BUCKET"] %>
production:
access_key_id: <%= ENV["MY_S3_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["MY_S3_SECRET_ACCESS_KEY"] %>
bucket: <%= ENV["MY_S3_BUCKET"] %>
当我上传图片时,它似乎可以根据日志进行工作:
SQL (0.7ms) INSERT INTO "photos" ("created_at", "featured", "img_content_type", "img_file_name", "img_file_size", "img_fingerprint", "img_updated_at", "position", "property_id", "published", "source_uri", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["created_at", Sat, 25 May 2019 05:52:17 UTC +00:00], ["featured", nil], ["img_content_type", nil], ["img_file_name", "65798ASDASD4"], ["img_file_size", nil], ["img_fingerprint", nil], ["img_updated_at", nil], ["position", nil], ["property_id", 18713], ["published", true], ["source_uri", "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/31680-1.jpg"], ["updated_at", Sat, 25 May 2019 05:52:17 UTC +00:00]]
(0.9ms) COMMIT
但是,如果我要在控制台中四处浏览,将会得到以下信息:
irb(main):027:0> q.photos.first.source_uri
Photo Load (11.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."property_id" = 16759 ORDER BY position ASC LIMIT 1
=> "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/10192-16.jpg"
irb(main):028:0> p.photos.first.source_uri
=> "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/33449-7.jpg"
irb(main):029:0> q.photos.first.img.url
Photo Load (8.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."property_id" = 16759 ORDER BY position ASC LIMIT 1
=> "http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/photos/imgs/208532/original/9086718487708773"
irb(main):030:0> p.photos.first.img.url
=> "http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/photos/imgs/241108/original/7782737090666986"
请注意,最后一个代码段中的前两个URL有效,但最后2个返回“访问被拒绝”错误。
我还尝试使用Comfy CMS将头像上传到S3,并且有效。当然,我不认为这是由Paperclip管理的(我可能错了),但是由于某种原因,它会将其上传到其他地方。这是可以使用的头像的示例网址-http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/agents/avatars/21/large/10PP-COMING-SOON.jpg?1558767320
那么我的主要property.photos
模型可能出什么问题了?
那是为什么?
答案 0 :(得分:0)
也许您需要配置存储桶以允许跨源请求,创建CORS配置,这是一个XML文档,其中包含用于标识可以访问存储桶的源的规则,支持每个源的操作(HTTP方法)以及其他具体信息的操作。
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
来源:https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html