[英]Serving images remotely or directly the Amazon S3 - Dragonfly Gem
I have an application on Heroku and i am using Amazon S3 for storing images.我在 Heroku 上有一个应用程序,我正在使用 Amazon S3 来存储图像。 I have used all Cache technics i know but seems images load too slow and it's putting off some users.
我已经使用了我知道的所有缓存技术,但似乎图像加载速度太慢,并且让一些用户望而却步。
At the moment users get目前用户得到
/media/W1siZiIsIjIwMTQvMDIvMjEvMjMvMjAvMDQvNTY1L01pa2VfOTIzMi5qcGciXV0/Mike_150.jpg?sha=d8993be2
According to instructions one has to use根据说明,必须使用
Dragonfly.app.remote_url_for(uid)
and they will get他们会得到
http://my-bucket.s3.amazonaws.com/2011/04/01/03/03/05/243/file.jpg
However when i add this line, nothing happens.但是,当我添加这一行时,没有任何反应。
#require 'dragonfly/s3_data_store'
require 'dragonfly'
# Configure
Dragonfly.app.configure do
plugin :imagemagick
protect_from_dos_attacks true
secret "2558d89a83f18f6da793e3b6dccc888c17642563e9ddedf456356f4c2d79"
url_format '/media/:job/:name'
response_header 'Cache-Control', 'public, max-age=3600' # You can set custom response headers
response_header 'Cache-Control' do |job, request, headers| # either directly or with a block
job.image? ? "public, max-age=10000000" : "private" # setting to nil removes the header
end
allow_legacy_urls true
if Rails.env.test? || Rails.env.development?
datastore :file,
root_path: Rails.root.join('public/system/dragonfly', Rails.env),
server_root: Rails.root.join('public')
else
datastore :s3,
bucket_name: ENV['S3_BUCKET_NAME'],
access_key_id: ENV['S3_KEY'],
secret_access_key: ENV['S3_SECRET'],
url_scheme: 'http',
url_host: 'mybucket.s3.amazonaws.com'
end
end
# Logger
Dragonfly.logger = Rails.logger
Dragonfly.app.remote_url_for(uid)
# Mount as middleware
Rails.application.middleware.use Dragonfly::Middleware
# Add model functionality
if defined?(ActiveRecord::Base)
ActiveRecord::Base.extend Dragonfly::Model
ActiveRecord::Base.extend Dragonfly::Model::Validations
end
You are serving images from S3 directly.您直接从 S3 提供图像。 Depending on where in the world your user is vs the region of the S3 bucket the latency and speed might vary.
根据您的用户所在的世界与 S3 存储桶的区域,延迟和速度可能会有所不同。
One option to speed this up is to use AWS Cloudfront CDN and keep the S3 bucket as the origin server.加快速度的一种选择是使用 AWS Cloudfront CDN 并将 S3 存储桶保留为源服务器。
I have been struggling with this during a while.一段时间以来,我一直在为此苦苦挣扎。 I haven't found a clean solution unfortunately but I ended with something that works.
不幸的是,我还没有找到一个干净的解决方案,但我最终得到了一些有效的解决方案。
Composing your own url string would do the job:编写您自己的 url 字符串可以完成这项工作:
url = 'https://YOURBUCKET.s3.eu-central-1.amazonaws.com/' + picture.image_file_uid
I hope this can help somebody.我希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.