Rails 6,Paperclip,S3,s3_direct_upload @attachment.save 在 s3_direct_upload 完成后不保存
Posted
技术标签:
【中文标题】Rails 6,Paperclip,S3,s3_direct_upload @attachment.save 在 s3_direct_upload 完成后不保存【英文标题】:Rails 6, Paperclip, S3, s3_direct_upload @attachment.save not saving after the s3_direct_upload completes 【发布时间】:2021-12-23 07:30:27 【问题描述】:我已经将一个名为 FilterTrak 的应用程序从旧版本的 Rails v4.2 重新编写到 Rails v6,现在除了上传图片的一部分外,我现在一切正常。
该应用程序使用 Paperclip、aws-sdk-v1 和 s3_direct_upload。我以前遇到过 s3_direct_upload 的问题,我开始工作了。我现在遇到的问题是,在将文件上传到 s3 后,它应该保存附件,这将为我提供数据库中的 ID,然后调用应该启动的辅助进程。
由于未知原因,@attachment.save 没有保存,因为它没有保存,所以它没有回调第二个进程,该进程应该将文件移动到 S3 上的永久位置,然后将该信息保存到D B。我曾尝试手动调用第二个进程,但由于附件未保存未生成 ID。由于我没有可使用的 ID,因此无法手动或以其他方式调用第二个进程。我相信我已经解决了控制器和@attachment.save 行的问题。如果我在那之后立即检查@attachment.save,我会得到@attachment.save FALSE。此代码目前在旧版本的 Rails 上正常工作。所以我认为这部分由于新版本的 Rails 发生了一些变化而无法正常工作。我花了两个多星期的时间试图弄清楚这一点,但我没有取得任何进展。我正在考虑更改应用程序以使用活动存储,但由于@attachment.save 是我认为不起作用的部分,我不知道这是否真的有任何好处,因为文件上传似乎不在哪里问题是据我所知。因为它实际上确实将我的 S3 存储桶上传到 /uploads 文件夹中。
这是 attachments_controller.rb 文件。
class AttachmentsController < ApplicationController
load_and_authorize_resource except: [:create]
skip_authorization_check only: [:create]
before_action :set_service, only: [:create, :update, :destroy]
before_action :set_attachment, only: [:update, :destroy]
def create!
@attachment = @service.attachments.new(attachment_params)
@attachment.when = params[:when]
@attachment.save
end
def update
respond_to do |format|
if @attachment.update(attachment_params)
format.html redirect_to @attachment, notice: 'Attachment was successfully updated.'
format.json render :show, status: :ok, location: @attachment
else
format.html render :edit
format.json render json: @attachment.errors, status: :unprocessable_entity
end
end
end
def destroy
@attachment.destroy
respond_to do |format|
format.html redirect_to customer_filter_service_path(@customer, @filter, @service), notice: 'Picture was successfully removed.'
format.json head :no_content
end
end
private
def set_service
@customer = Customer.find(params[:customer_id])
@filter = @customer.filters.find(params[:filter_id])
@service = @filter.services.find(params[:service_id])
#@attachment = Attachment.find(params[:id])
end
def set_attachment
@attachment = Attachment.find(params[:id])
end
def attachment_params
params.require(:attachment).permit(:direct_upload_url)
end
end
这是 attachments.rb 模型(将存储桶名称更改为 mybucket)
# == Schema Information
#
# Table name: attachments
#
# id :integer not null, primary key
# service_id :integer
# user_id :integer
# direct_upload_url :string not null
# upload_file_name :string
# upload_content_type :string
# upload_file_size :integer
# upload_updated_at :datetime
# processed :boolean default("false"), not null
# created_at :datetime not null
# updated_at :datetime not null
# when :string
#
class Attachment < ApplicationRecord
# Environment-specific direct upload url verifier screens for malicious posted upload locations.
DIRECT_UPLOAD_URL_FORMAT = %r\Ahttps:\/\/mybucket#!Rails.env.production? ? "\\-#Rails.env" : ''\.s3\.amazonaws\.com\/(?<path>uploads\/.+\/(?<filename>.+))\z.freeze
#attr_accessor :id, :service_id, :user_id, :direct_upload_url, :upload_file_name,
#:upload_content_type, :upload_file_size, :upload_updated_at, :processed, :created_at, :updated_at, :when
belongs_to :service
belongs_to :user
has_attached_file :upload,
path: 'service/:filter_id/attachments/:id-:style-:filename',
styles: medium: "200x200"
validates :direct_upload_url, presence: true, format: with: DIRECT_UPLOAD_URL_FORMAT
validates_attachment_content_type :upload, :content_type => ["image/jpg", "image/jpeg", "image/png"]
validates_presence_of :when
before_create :set_upload_attributes
after_create :queue_processing
# interpolate in paperclip
Paperclip.interpolates :filter_id do |p, _|
p.instance.service.filter_id
end
# Store an unescaped version of the escaped URL that Amazon returns from direct upload.
def direct_upload_url=(escaped_url)
write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil))
end
# Determines if file requires post-processing (image resizing, etc)
def post_process_required?
%r^(image|(x-)?application)/(jpeg|jpg|pjpeg|png|x-png)$.match(upload_content_type).present?
end
# Final upload processing step
def self.transfer_and_cleanup(id)
attachment = Attachment.find(id)
direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(attachment.direct_upload_url)
s3 = AWS::S3.new
if attachment.post_process_required?
# this has an issue removing brackets
attachment.upload = URI.parse(URI.escape(attachment.direct_upload_url))
else
paperclip_file_path = "service/filter-#filter_id/service-#service_id/photos/#direct_upload_url_data[:filename]"
s3.buckets[Rails.configuration.aws[:bucket]].objects[paperclip_file_path].copy_from(direct_upload_url_data[:path])
end
attachment.processed = true
attachment.save
s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].delete
end
def before_install?
self.when == "before"
end
def after_install?
self.when == "after"
end
protected
# Set attachment attributes from the direct upload
# @note Retry logic handles S3 "eventual consistency" lag.
def set_upload_attributes
tries ||= 5
direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(direct_upload_url)
s3 = AWS::S3.new
direct_upload_head = s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].head
self.upload_file_name = direct_upload_url_data[:filename]
self.upload_file_size = direct_upload_head.content_length
self.upload_content_type = direct_upload_head.content_type
self.upload_updated_at = direct_upload_head.last_modified
rescue AWS::S3::Errors::NoSuchKey => e
tries -= 1
if tries > 0
sleep(3)
retry
else
false
end
end
# Queue file processing
def queue_processing
# Attachment.delay.transfer_and_cleanup(id)
Attachment.transfer_and_cleanup(id)
# bust service cache
service.touch
end
scope :before_install, -> where( when: "before" )
scope :after_install, -> where( when: "after" )
end
这里是视图文件的相关部分:
%br.visible-print/
%br.visible-print/
.panel.panel-default.page-break
.panel-heading
.row
.col-md-6
.pull-left
Pictures
.panel-body
.row
.col-md-12
.row
.col-md-12
%h4.strong Before Cleaning Photos
.row.hidden-print
.col-md-12
.well#before-pictures-dropzone.dropzone
%h4 Click to Add
= s3_uploader_form id: "attachment_before",
callback_url: customer_filter_service_attachments_url(@customer, @filter, @service, when: "before"),
callback_param: "attachment[direct_upload_url]",
key_starts_with: "uploads/",
key: "uploads/timestamp-unique_id-#SecureRandom.hex/$filename",
max_file_size: 100.megabytes do
= file_field_tag(:file, class: "attachment", multiple: true, data: url: s3_uploader_url )
#before_uploads_container
%script#template-upload:type => "text/x-tmpl"
<div id="upload_%=o.unique_id%" class="upload">
<h5>%=o.name%</h5>
<div class="progress progress-striped active"><div class="bar" style="width: 0%"></div></div>
</div>
.row
.col-md-12
- if @before_photos
= render @before_photos, gallery: "before"
- else
%h5 No Photos Added Yet
#before_photos_container
这是路由文件中处理该部分的部分:
resources :customers do
resources :filters, except: [:index, :show] do
resources :services, except: [:index, :show] do
post 'email', on: :member
resources :attachments, except: [:index, :show]
end
end
end
这是在您单击“选择文件”按钮并选择您的一个或多个文件后开始上传过程,然后再次将 1 个文件或 10 个文件成功上传到 S3 到 /uploads 文件夹。进度条出现、构建、完成,然后按预期消失。第二个进程永远不会启动。
Started POST "/customers/419/filters/990/services/1137/attachments?when=before" for ::1 at 2021-11-10 09:07:42 -0800
Processing by AttachmentsController#create as */*
Parameters: "url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filepath"=>"/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>"direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "when"=>"before", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137"
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 34], ["LIMIT", 1]]
Customer Load (1.0ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT $2 /*line:/app/controllers/attachments_controller.rb:42:in `set_service'*/ [["id", 419], ["LIMIT", 1]]
↳ app/controllers/attachments_controller.rb:42:in `set_service'
Filter Load (0.9ms) SELECT "filters".* FROM "filters" WHERE "filters"."customer_id" = $1 AND "filters"."id" = $2 LIMIT $3 /*line:/app/controllers/attachments_controller.rb:43:in `set_service'*/ [["customer_id", 419], ["id", 990], ["LIMIT", 1]]
↳ app/controllers/attachments_controller.rb:43:in `set_service'
Service Load (1.0ms) SELECT "services".* FROM "services" WHERE "services"."filter_id" = $1 AND "services"."id" = $2 LIMIT $3 /*line:/app/controllers/attachments_controller.rb:44:in `set_service'*/ [["filter_id", 990], ["id", 1137], ["LIMIT", 1]]
↳ app/controllers/attachments_controller.rb:44:in `set_service'
Attachment Load (0.9ms) SELECT "attachments".* FROM "attachments" WHERE "attachments"."service_id" = $1 /*line:/app/controllers/attachments_controller.rb:11:in `create'*/ [["service_id", 1137]]
↳ app/controllers/attachments_controller.rb:11:in `create'
:: RIGHT HERE IS WHERE A SECOND PROCCESS IS SUPPOSED TO START ::
Rendering attachments/create.js.erb
Rendered attachments/create.js.erb (Duration: 0.1ms | Allocations: 8)
Completed 200 OK in 29ms (Views: 1.6ms | ActiveRecord: 4.8ms | Allocations: 10698)
这是第二个进程应该是什么,这是从当前在 Heroku 上运行的应用程序中删除的。
2021-10-25T23:13:27.756616+00:00 app[web.1]: (0.5ms) BEGIN
2021-10-25T23:13:27.802287+00:00 app[web.1]: [AWS S3 200 0.044684 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:27.802289+00:00 app[web.1]:
2021-10-25T23:13:27.805869+00:00 app[web.1]: SQL (2.3ms) INSERT INTO "attachments" ("direct_upload_url", "service_id", "when", "created_at", "updated_at", "upload_file_name", "upload_file_size", "upload_content_type", "upload_updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["direct_upload_url", "https://dpfregen.s3.amazonaws.com/uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg"], ["service_id", 1137], ["when", "before"], ["created_at", "2021-10-25 23:13:27.756915"], ["updated_at", "2021-10-25 23:13:27.756915"], ["upload_file_name", "DES LOGO (512x512).jpg"], ["upload_file_size", 86383], ["upload_content_type", "image/jpeg"], ["upload_updated_at", "2021-10-25 23:13:28.000000"]]
2021-10-25T23:13:27.807045+00:00 app[web.1]: Attachment Load (0.7ms) SELECT "attachments".* FROM "attachments" WHERE "attachments"."id" = $1 LIMIT 1 [["id", 106905]]
2021-10-25T23:13:27.834504+00:00 app[web.1]: Service Load (0.9ms) SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1 [["id", 1137]]
2021-10-25T23:13:27.852128+00:00 app[web.1]: [AWS S3 404 0.016364 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-original-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.852129+00:00 app[web.1]:
2021-10-25T23:13:27.869707+00:00 app[web.1]: [AWS S3 404 0.017152 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-medium-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.869708+00:00 app[web.1]:
2021-10-25T23:13:27.871399+00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-1mcjn6k.jpg'
2021-10-25T23:13:27.874638+00:00 app[web.1]: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' 2>/dev/null
2021-10-25T23:13:27.891231+00:00 app[web.1]: Command :: identify -format %m '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]'
2021-10-25T23:13:27.896884+00:00 app[web.1]: Command :: convert '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' -auto-orient -resize "200x200" '/tmp/4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y'
2021-10-25T23:13:27.945653+00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-w4tdp8.jpg'
2021-10-25T23:13:27.951181+00:00 app[web.1]: SQL (0.8ms) UPDATE "attachments" SET "upload_file_name" = $1, "upload_updated_at" = $2, "processed" = $3, "updated_at" = $4 WHERE "attachments"."id" = $5 [["upload_file_name", "DES_20LOGO_20(512x512).jpg"], ["upload_updated_at", "2021-10-25 23:13:27.870718"], ["processed", "t"], ["updated_at", "2021-10-25 23:13:27.948459"], ["id", 106905]]
2021-10-25T23:13:27.951557+00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:27.986837+00:00 app[web.1]: [AWS S3 200 0.034825 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>86383,:content_type=>"image/jpeg",:data=>Paperclip::UriAdapter: DES%20LOGO%20(512x512).jpg,:key=>"service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:27.986839+00:00 app[web.1]:
2021-10-25T23:13:27.986964+00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:28.017915+00:00 app[web.1]: [AWS S3 200 0.029985 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>40823,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y,:key=>"service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:28.017923+00:00 app[web.1]:
2021-10-25T23:13:28.044967+00:00 app[web.1]: [AWS S3 204 0.026224 0 retries] delete_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:28.044969+00:00 app[web.1]:
2021-10-25T23:13:28.047863+00:00 app[web.1]: SQL (0.8ms) UPDATE "services" SET "updated_at" = '2021-10-25 23:13:28.045149' WHERE "services"."id" = $1 [["id", 1137]]
2021-10-25T23:13:28.050243+00:00 app[web.1]: SQL (0.6ms) UPDATE "filters" SET "updated_at" = '2021-10-25 23:13:28.048146' WHERE "filters"."id" = $1 [["id", 990]]
2021-10-25T23:13:28.051630+00:00 app[web.1]: Company Load (0.6ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1 [["id", 1]]
2021-10-25T23:13:28.054209+00:00 app[web.1]: SQL (0.6ms) UPDATE "companies" SET "updated_at" = '2021-10-25 23:13:28.052156' WHERE "companies"."id" = $1 [["id", 1]]
2021-10-25T23:13:28.056432+00:00 app[web.1]: (1.9ms) COMMIT
:: THEN IT GOES TO THE ENDING SECTION JUST LIKE MY CURRENT APP IS DOING BUT MY CURRENT APP IS SKIPPING ALL OF THE ABOVE ::
2021-10-25T23:13:28.060278+00:00 app[web.1]: Rendered attachments/_attachment.html.haml (2.6ms)
2021-10-25T23:13:28.060371+00:00 app[web.1]: Rendered attachments/create.js.erb (3.0ms)
2021-10-25T23:13:28.060540+00:00 app[web.1]: Completed 200 OK in 312ms (Views: 3.8ms | ActiveRecord: 12.1ms)
这是@service.attachments 的转储
@service.attachments: #<ActiveRecord::Associations::CollectionProxy [#<Attachment id: 3391, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141838.jpg", upload_content_type: "image/jpeg", upload_file_size: 664048, upload_updated_at: "2016-06-29 14:37:55.771560000 -0700", processed: true, created_at: "2016-06-29 14:37:55.511793000 -0700", updated_at: "2016-06-29 14:37:55.948425000 -0700", when: "before">, #<Attachment id: 3389, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141808.jpg", upload_content_type: "image/jpeg", upload_file_size: 624308, upload_updated_at: "2016-06-29 14:37:42.278770000 -0700", processed: true, created_at: "2016-06-29 14:37:42.134005000 -0700", updated_at: "2016-06-29 14:37:42.484505000 -0700", when: "before">, #<Attachment id: 3390, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141758.jpg", upload_content_type: "image/jpeg", upload_file_size: 675360, upload_updated_at: "2016-06-29 14:37:53.449659000 -0700", processed: true, created_at: "2016-06-29 14:37:53.068395000 -0700", updated_at: "2016-06-29 14:37:53.671685000 -0700", when: "before">, #<Attachment id: 3437, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673121...", upload_file_name: "1467312187074-1193375171.jpg", upload_content_type: "image/jpeg", upload_file_size: 639235, upload_updated_at: "2016-06-30 11:42:58.287106000 -0700", processed: true, created_at: "2016-06-30 11:42:57.996008000 -0700", updated_at: "2016-06-30 11:42:59.051767000 -0700", when: "after">, #<Attachment id: 3439, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673122...", upload_file_name: "1467312251285-1977276610.jpg", upload_content_type: "image/jpeg", upload_file_size: 877201, upload_updated_at: "2016-06-30 11:44:01.036497000 -0700", processed: true, created_at: "2016-06-30 11:44:00.642325000 -0700", updated_at: "2016-06-30 11:44:01.653891000 -0700", when: "after">,
:: THIS BELOW IS THE FILE THAT WAS JUST UPLOADED, NOTICE THE attachment_id is NILL, this is because it hasn't been saved ::
#<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>]>
这是同一时间点的@attachment:
@attachment: #<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>
这是同时转储的参数:
params#<ActionController::Parameters "url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filepath"=>"/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>#<ActionController::Parameters "direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads%2F1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926%2FDES+LOGO+%28512x512%29.jpg" permitted: true>, "when"=>"before", "controller"=>"attachments", "action"=>"create", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137" permitted: true>
如果我检查 @attachment.save 我会得到:
@attachment.save:false
据我所知,在 after_create 回调上调用的第二个进程 :queue_processing 未启动的原因是 @attachement.save 未保存。为什么它不保存我不明白。我尝试了各种手动方式来尝试强制保存但它不起作用,我尝试添加@attachment = Attachment.save,我尝试添加@attachment = Attachment.create(attachment_params),我尝试添加@attachment = Attachment.create(attachment_params[:id]) 都在 @attachment.save 之前,无论我做什么它都不会保存,我只是不明白为什么它不会保存。旧版本的 rails 上的相同代码确实可以保存,并且可以在那时运行。
如果有人有任何想法,我很想听听,并且非常感激。我假设问题来自@attachment.save,但我想也可能是 s3_direct_upload 没有正确进行回调,但我不知道如何解决该部分的问题。我在 js 文件中的 console.log 语句中做了,它确实到达了正确的位置,据我所知,它确实触发了 s3_direct_upload 过程,因为我在每个步骤和每个步骤中都将各种 console.log 条目放入其中据我所知,工作正常。
我还尝试升级到 aws-sdk(第 3 版)和 kt-paperclip 的更高版本,它做的事情完全相同,所以我可以说这似乎不是回形针或 aws 的故障.我什至愿意弄清楚如何手动从数据库中获取下一个 ID,然后在 @attachment.save 之后手动调用 queue_proccessing(我尝试过这样做)。我无法让它工作的唯一原因是@attachment.save 没有保存,所以它没有返回 ID,没有 ID,我无法手动调用它。
非常感谢任何帮助。我什至愿意雇用或支付某人的帮助来解决这个问题,因为我非常困惑并且需要了解这个问题的答案。我有第二个应用程序在同一个位置以同样的方式失败。这个其他应用程序也是从更旧版本的 Rails v3.2 重写为当前版本。这是我唯一无法修复的部分。
如果有人想让我发布任何其他内容,请告诉我,我将非常乐意发布任何其他对任何人有帮助的内容。
谢谢你, 斯科特
【问题讨论】:
【参考方案1】:开启@attachment.save 试试@attachment.valid?检查对象是否有效 如果对象无效,则通过 @attachment.errors.full_messages 检查错误消息 您可以清楚地看到缺失和无效的属性
这里 " @attachment: #" 您可以看到 user_id 缺少 set user id 然后重试 我希望在此之后您的问题得到解决
【讨论】:
这解决了我的问题,谢谢Zahid,非常感谢您的帮助!以上是关于Rails 6,Paperclip,S3,s3_direct_upload @attachment.save 在 s3_direct_upload 完成后不保存的主要内容,如果未能解决你的问题,请参考以下文章
使用 Rails 和 Paperclip 设置存储桶的名称放置域样式 (bucket.s3.amazonaws.com)
Rails:Amazon S3 + Paperclip 不在数据库中存储任何内容?