我正在构建一个PDF解析器,它可以触发Sidekiq worker从存储在S3中的文档中OCR解析数据。解析后,数据存储在文档模型中。
如何在不复制文件的情况下将现有的S3存储桶文件附加到ActiveStorage中的Document.attachment.attach中(通过File.open等)在S3?
发布于 2018-10-28 04:05:38
这可以通过在创建blob之后对其进行轻微的操作来完成。
storage.yml
amazon:
service: S3
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
region: <%= ENV['AWS_REGION'] %>
bucket: <%= ENV['S3_BUCKET'] %>app/models/document.rb
class Document < ApplicationRecord
has_one_attached :pdf
endrails控制台
key = "<S3 Key of the existing file in the same bucket that storage.yml uses>"
# Create an active storage blob that will represent the file on S3
params = {
filename: "myfile.jpg",
content_type:"image/jpeg",
byte_size:1234,
checksum:"<Base 64 encoding of the MD5 hash of the file's contents>"
}
blob = ActiveStorage::Blob.create_before_direct_upload!(params)
# By default, the blob's key (S3 key, in this case) a secure (random) token
# However, since the file is already on S3, we need to change the
# key to match our file on S3
blob.update_attributes key:key
# Now we can create a document object connected to your S3 file
d = Document.create! pdf:blob.signed_id
# in your view, you can now use
url_for d.pdf此时,您可以像使用任何其他活动存储附件一样使用Document对象的pdf属性。
发布于 2019-01-30 03:41:32
特洛伊的回答对我很有效!我还发现从对象的s3实例中提取关于该对象的元数据很有帮助。类似于:
s3 = Aws::S3::Resource.new(region: "us-west-1")
obj = s3.bucket("my-bucket").object("myfile.jpg")
params = {
filename: obj.key,
content_type: obj.content_type,
byte_size: obj.size,
checksum: obj.etag.gsub('"',"")
}我只有46分,所以我留下了这个答案,而不是评论:/
https://stackoverflow.com/questions/52323977
复制相似问题