0
我正在使用Paperclip和aws-sdk gems創建rails API應用程序。如何將S3存儲區URL更改爲先存取然後再存入url
該應用程序將URL保存爲字符串。保存的網址如下。
http://s3.amazonaws.com/S3_BUCKET_/profiles/avatars/000/000/001/original/avatar.png?1457514823
我無法打開上面的圖像。就是因爲從S3拍攝時它是以下URL
http://S3_BUCKET_/s3.amazonaws.com//profiles/avatars/000/000/001/original/avatar.png?1457514823
看到水桶是如何第一?但保存在數據庫中的url有第二個?如何更改已保存的網址以便首先使用存儲區?
config/initializers/paperclip.rb
Paperclip::Attachment.default_options.update(
default_url: "https://#{Rails.application.secrets.bucket}.s3-ap-southeast-2.amazonaws.com/" \
"/profiles/avatars/default/missing.jpg")
config/aws.yml
development: &defaults
access_key_id: s3_access_key
secret_access_key: s3 secret key
s3_region: ap-southeast-2
test:
secret_access_key: s3 secret key
staging:
<<: *defaults
access_key_id: s3_access_key
secret_access_key: <%= ENV["SECRET_KEY_BASE"] %>
production:
<<: *defaults
access_key_id: s3_access_key
secret_access_key: <%= ENV["SECRET_KEY_BASE"] %>
profile.rb it has the attachment saved
require "base64"
class Profile < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
has_attached_file :avatar, styles: { thumb: "100x100>" }
validates_attachment_content_type :avatar, content_type: /image/i
def avatar_url
avatar && avatar.url
end
def avatar_base64=(image_base64)
file = Paperclip.io_adapters.for(image_base64)
file.original_filename = file.content_type.sub("image/", "avatar.")
self.avatar = file
end