我已经为此奋斗了一段时间,您的帮助肯定会非常感激。
我已经建立了一种签署 pdf 文档的方法,您可以在此处找到,现在离签署我的文件只有一步之遥。
我想异步执行此操作,但首先我需要了解如何同步执行此操作。
所以我尝试了两种不同的方法,在 post_process 之后:
after_post_process do |receipt|
if receipt.receipt_file_changed?
require 'aws-sdk'
logger.debug("RECEIPT ID: #{self.inspect}")
file = receipt.receipt_file.queued_for_write[:original]
s3=AWS::S3.new(
access_key_id: S3_CONFIG["access_key_id"],
secret_access_key: S3_CONFIG["secret_access_key"])
bucket_name = S3_CONFIG["bucket"]
b = s3.buckets[bucket_name]
filen = File.basename(file.path)
outputF = "original/teste.pdf"
o = b.objects[outputF]
o.write(file: file.path)
end
end
我希望能够将文件发送到类似于/original/1/myfilename.pdf
1 将是我的receipt_id 的路径(并且在 post_process 之后为空)。
然后我用 after_save 尝试了一种不同的方法,但是receipt_file.to_file(:original) 不是一个有效的调用......
after_save do |receipt|
if receipt.receipt_file_changed?
require 'aws-sdk'
logger.debug("RECEIPT ID: #{receipt.inspect}")
s3=AWS::S3.new(
access_key_id: S3_CONFIG["access_key_id"],
secret_access_key: S3_CONFIG["secret_access_key"])
bucket_name = S3_CONFIG["bucket"]
b = s3.buckets[bucket_name]
filen = File.basename(receipt_file_file_name)
outputF = "original/teste.pdf"
o = b.objects[outputF]
o.write(file: receipt.receipt_file.to_file(:original))
end
end
如何获取文件并将其再次上传回 S3?
编辑
经过一些研究,我阅读了如何从亚马逊加载文件,现在的问题是我的文件内容是空的......我做错了什么?
after_save do |receipt|
if receipt.receipt_file_changed?
require 'aws-sdk'
logger.debug("I was here inside after_save")
s3=AWS::S3.new(
access_key_id: S3_CONFIG["access_key_id"],
secret_access_key: S3_CONFIG["secret_access_key"])
bucket_name = S3_CONFIG["bucket"]
b = s3.buckets[bucket_name]
filen = File.basename(receipt_file_file_name)
logger.debug("Filename is #{filen}")
path = "original/#{receipt.id}/#{filen}"
o = b.objects[path]
require 'tempfile'
ext= File.extname(filen)
file = Tempfile.new([File.basename(filen,ext),ext], :encoding => 'ascii-8bit')
# streaming download from S3 to a file on disk
begin
o.read do |chunk|
file.write(chunk)
end
end
file.close
logger.debug("File is #{file.inspect}")
o.write(file: signPdf(file).path)
file.unlink
end
end