问题描述
我正在关注uploading multiple files和direct S3 uploads的Shrine文档,但它们没有按预期工作,我也不明白为什么。 文件在params请求中以files[]
的形式提交,但选择后应上传到S3。相反,唯一发生的提交是单击“提交”以将其他所有内容保存到数据库时发生的POST请求。为什么这不起作用?谢谢您抽出宝贵的时间来研究这个问题!
Processing by ItemsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"[FILTERED]","files"=>[#<Actiondispatch::Http::UploadedFile:0x00007f9cbc59a050 u/tempfile=#<Tempfile:/var/folders/hj/g_90jx_n7s58m73qlf9h0c4w0000gn/T/RackMultipart20200924-5853-1pb0sdf.jpg>,u/original_filename="in.jpg",u/content_type="image/jpeg",u/headers="Content-disposition: form-data; name=\"files[]\"; filename=\"in.jpg\"\r\nContent-Type: image/jpeg\r\n">],"item"=>{"title"=>"dip"},"commit"=>"Submit"}
Item Create (10.1ms) INSERT INTO "items" ("created_at","updated_at","title","user_id") VALUES ($1,$2,$3,$4) RETURNING "id" [["created_at","2020-09-24 22:04:41.333195"],["updated_at",["title","dip"],["user_id",1]]
我的初始化程序:
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/s3"
s3 = {
bucket: ENV['S3_BUCKET_NAME'],region: ENV['AWS_REGION'],access_key_id: ENV['AWS_ACCESS_KEY_ID'],secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache",**s3),store: Shrine::Storage::S3.new(prefix: "store",**s3)
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :validation
Shrine.plugin :validation_helpers
Shrine.plugin :determine_mime_type,analyzer: :marcel
Shrine.plugin :remove_invalid
我的上传者:
class Imagesuploader < Shrine
require "image_processing/mini_magick"
plugin :pretty_location
plugin :derivatives
Shrine.plugin :presign_endpoint,presign_options: -> (request) {
filename = request.params["filename"]
type = request.params["type"]
{
content_disposition: Contentdisposition.inline(filename),content_type: type,content_length_range: 0..(5*1024*1024),}
}
Attacher.validate do
validate_min_size 10.kilobytes
validate_max_size 5.megabytes,message: 'must be smaller than 5MB'
validate_mime_type %w[image/jpeg image/png]
validate_extension %w[jpg jpeg png]
end
Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
large: magick.resize_to_limit!(1000,1000),small: magick.resize_and_pad!(225,220)
}
end
end
多态照片模型:
create_table "photos",force: :cascade do |t|
t.string "imageable_type"
t.bigint "imageable_id"
t.text "image_data"
t.datetime "created_at",null: false
t.datetime "updated_at",null: false
t.index ["imageable_type","imageable_id"],name: "index_photos_on_imageable_type_and_imageable_id"
end
型号:
class Item < ApplicationRecord
has_many :photos,as: :imageable,dependent: :destroy
end
class Photo < ApplicationRecord
include Imagesuploader::Attachment(:image)
belongs_to :imageable,polymorphic: true
validates_presence_of :image
end
视图:
<%= form_for @item,html: { enctype: "multipart/form-data" } do |f| %>
<%= f.fields_for :photos do |i| %>
<%= i.label :image %>
<%= i.hidden_field :image,value: i.object.cached_photos_data,class: "upload-data" %>
<%= i.file_field :image,class: "upload-file" %>
<% end %>
<%= file_field_tag "files[]",multiple: true %>
<%= f.text_field :title %>
<%= f.submit "Submit" %>
<% end %>
我的路线并预告控制器动作以进行授权:
get 'presign/s3/params',to: 'presigns#image'
class PresignsController < InheritedResources::Base
before_action :authenticate_user!
def image
set_rack_response Imagesuploader.presign_response(:cache,request.env)
end
private
def set_rack_response((status,headers,body))
self.status = status
self.headers.merge!(headers)
self.response_body = body
end
end
我的项目控制器:
class ItemsController < ApplicationController
def create
@item = current_user.owned_items.create(item_params)
@item.save
end
private
def item_params
params.require(:item).permit(:title,photos_attributes: { image: [] })
end
end
然后所有的webpacker code for fileUpload.js,除了我改变了这一行...
.use(AwsS3,{
companionUrl: '/',})
...到...
.use(AwsS3,{
companionUrl: '/presign/s3/params',})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)