【发布时间】:2014-06-09 03:54:58
【问题描述】:
刚刚添加了使用 gem s3_direct_upload 直接上传到 Amazon S3 的功能。
不幸的是,现在它没有像我这样处理额外的图像大小调整。
:styles => { :thumbnail => "200x200#" }
查看文档后,我找不到任何与此问题相关的内容。我还使用了这个tutorial 来正确设置每一个。如何让图像样式与 direct_upload 一起使用到 S3??
日志
Processing by UpdatesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"fmxSfjg53jcouwmF/fdsoqxmDuq84=", "update"=>{"description"=>"", "direct_upload_url"=>"https://my-bucket.s3.amazonaws.com/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png", "image_file_name"=>"retro.png", "image_file_size"=>"107715", "image_content_type"=>"image/png", "image_file_path"=>"/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"}, "commit"=>"Save changes"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 29 ORDER BY "users"."id" ASC LIMIT 1
(0.3ms) BEGIN
Update Exists (0.5ms) SELECT 1 AS one FROM "updates" WHERE "updates"."id" = 730128 ORDER BY created_at DESC LIMIT 1
SQL (12.2ms) INSERT INTO "updates" ("created_at", "description", "direct_upload_url", "id", "image_content_type", "image_file_name", "image_file_path", "image_file_size", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", Wed, 23 Apr 2014 22:34:29 CDT -05:00], ["description", ""], ["direct_upload_url", "https://my-bucket.s3.amazonaws.com/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"], ["id", 730128], ["image_content_type", "image/png"], ["image_file_name", "retro.png"], ["image_file_path", "/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"], ["image_file_size", 107715], ["updated_at", Wed, 23 Apr 2014 22:34:29 CDT -05:00], ["user_id", 29]]
Digest::Digest is deprecated; use Digest
[AWS S3 300 .5453 0 retries] copy_object(:bucket_name=>"my-bucket",:copy_source=>"my-bucket/uploads/1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8/retro.png",:key=>"updates/images/730128/original/retro.png",:metadata_directive=>"COPY",:storage_class=>"STANDARD")
Digest::Digest is deprecated; use Digest
[AWS S3 543 0.0493 0 retries] delete_object(:bucket_name=>"my-bucket",:key=>"uploads/1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8/retro.png")
模型
class Update < ActiveRecord::Base
...
...
has_attached_file :image, :s3_protocol => :https,
:storage => :s3,
:styles => {
:profile => "550x330#" },
:convert_options => {
:thumb => "-quality 75 -strip" },
:s3_credentials => "#{Rails.root}/config/aws.yml",
:path => ":class/:attachment/:id/:style/:filename",
:url => ":s3_domain_url"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
def self.copy_and_delete(paperclip_file_path, raw_source)
s3 = AWS::S3.new #create new s3 object
destination = s3.buckets['bucket-name'].objects[paperclip_file_path]
sub_source = CGI.unescape(raw_source)
sub_source.slice!(0) # the attached_file_file_path ends up adding an extra "/" in the beginning. We've removed this.
source = s3.buckets['bucket-name'].objects["#{sub_source}"]
source.copy_to(destination) #copy_to is a method originating from the aws-sdk gem.
source.delete #delete temp file.
end
控制器
def create
if(params[:url])
@update = Update.new
render "new" and return
end
if(params[:update][:image_file_path])
@update = current_user.updates.create(update_params)
respond_to do |format|
if @update.save
#we want a destination(paperclip_file_path) and a source(raw_source)
paperclip_file_path = "updates/images/#{@update.id}/original/#{params[:update][:image_file_name]}"
raw_source = params[:update][:image_file_path]
Update.copy_and_delete paperclip_file_path, raw_source #this is where we call a method to copy from temp location to where paperclip expects it to be.
format.html { redirect_to update_path(:id => @update.id), :flash => { :success => "<strong>Awesome!</strong> You've successfully created your update.".html_safe }}
format.json { render action: 'show', status: :created, location: @update }
else
format.html { render action: 'new' }
format.json { render json: @update.errors, status: :unprocessable_entity }
end
end
else
@update = Update.new
render action: "new", notice: "No file"
end
end
【问题讨论】:
-
请在此处输入一些日志信息。没有这个就很难找到解决方案。
-
您使用的是哪个版本的
s3_direct_uploadgem?此错误已在 3 月 13 日发布的 v0.1.7 中修复。 github.com/waynehoover/s3_direct_upload/pull/149 -
啊。你让我充满希望。我正在使用
s3_direct_upload (0.1.7) -
@Justin 我的回答解决了您的问题吗?让我知道结果和查询(如果有)。
-
我在使用 rackspace 时遇到了同样的问题,使用雾添加 rackspace 支持后生成样式停止工作。
标签: ruby-on-rails ruby-on-rails-4 amazon-s3 paperclip