我使用 s3_direct_upload Gem 遵循了给定的说明。当我提交带有附加图像的上传表单时,会创建一个新的“上传”项目,但没有图像发送到 S3(或返回显示)。
我通过 Devise 有一个 user.rb 模型,但我用于图像的模型是 upload.rb。
这是提交图像表单时我在开发模式下获得的日志。您可以看到除了创建日期和 id 之外,没有任何内容附加到新的“上传”项目(其他所有内容均为零)。旁注 - 我是否应该在此日志中看到我的存储桶名称(我没有)?
附加信息:在我的 s3 控制台中,我删除了存储桶。我仍然在下面得到相同的输出。那么,似乎它甚至没有将信息发送到 s3?有什么想法可能导致这种情况吗?
在 2013 年 5 月 12 日 19:10:52 -0400 为 127.0.0.1 开始 POST "/uploads" 由 UploadsController#create as /处理 参数:{"utf8"=>"√", "key"=>"uploads/1368400251964-ol9ja4ijq0110pb9-bf206 97a98bbff7c58a83d7bfaa84800/${filename}", "acl"=>"public-read", "AWSAccessKeyId" =>" AKIAI2EXMFCUTILIGG", "policy"=>"eyJleHBpcmF0aW9uIjoiMjAxMy0wNS0xM1QwOToxMDo 0N1oiLCJjb25kaXRpb25zIjpbWyJzdGFydHMtd2l0aCIsIiR1dGY4IiwiIl0sWyJzdGFydHMtd2l0aCI sIiRrZXkiLCJ1cGxvYWRzLyJdLFsic3RhcnRzLXdpdGgiLCIkeC1yZXF1ZXN0ZWQtd2l0aCIsIiJdLFs iY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsNTI0Mjg4MDAwXSxbInN0YXJ0cy13aXRoIiwiJGNvbnRlbnQ tdHlwZSIsIiJdLHsiYnVja2V0IjoibGl2ZXBlcmNoIn0seyJhY2wiOiJwdWJsaWMtcmVhZCJ9LHsic3V jY2Vzc19hY3Rpb25fc3RhdHVzIjoiMjAxIn1dfQ==", "signature"=>"f9R5QeLx4nllEKug805CV1 OJTu0=", "success_action_status"=>"201", "X-Requested-With"=>"xhr", "content-type e"=>"image/jpeg", "file"=>#>} ←[1m←[36mUser Load (1.0ms)← [0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1←[0m ←[1m←[35m (0.0ms)←[0m BEGIN
←[1m←[36mSQL (81.0ms)←[0m ←[1mINSERT INTO "uploads" ("created_at", "image_url", "name", "product_id", "updated_at") 值 ($1, $2, $3, $4 , $5) 返回 "id"←[0m [["created_at", Sun, 12 May 2013 23:10:52 UTC +00:00], ["image_url", nil] , ["name", nil], [ "product_id", nil], ["updated_at", Sun, 12 May 2013 23:10:52 UTC +00:00]] ←[1m←[35m (18.0ms)←[0m COMMIT 渲染上传/_upload.html。 erb (1.0ms)
Rendered uploads/create.js.erb (4.0ms) Completed 200 OK in 127ms (Views: 22.0ms | ActiveRecord: 101.0ms)
这是 mu upload.rb 模型。
class Upload < ActiveRecord::Base
attr_accessible :image_url, :name, :product_id
before_create :default_name
def default_name
self.name ||= File.basename(image_url, '.*').titleize if image_url
end
end
这是触发发送到 s3 的图像表单代码。
<%= s3_uploader_form callback_url: uploads_url, callback_param: upload[image_url]", id: "myS3Uploader" do %>
<%= file_field_tag :file, multiple: true %>
<% end %>
这是 Gem 需要包含我的 s3 信息的初始化程序文件。我编辑了密钥和存储桶名称。
S3DirectUpload.config do |c|
c.access_key_id = "AWS_KEY" # your access key id
c.secret_access_key = "AWS_SECRET_KEY" # your secret access key
c.bucket = "AWS_BUCKET_NAME" # your bucket name
c.region = "" # region prefix of your bucket url (optional), eg. "s3-eu-west-1"
c.url = "" # S3 API endpoint (optional), eg. "https://#{c.bucket}.s3.amazonaws.com/"
end
而且,这里是模型的 create.js.erb 文件
<% if @upload.new_record? %>
alert("Failed to upload: <%= j @upload.errors.full_messages.join(', ').html_safe %>");
<% else %>
$("#uploads").append("<%= j render(@upload) %>");
<% end %>
我对rails有点陌生,这让我发疯了。我很感激任何帮助!