Amazon s3 现有S3文件的Rails ActiveStorage附件
我正在构建一个PDF解析器,它让Sidekiq工作人员从存储在S3中的文档中对数据进行OCR解析。解析后,数据存储在文档模型中Amazon s3 现有S3文件的Rails ActiveStorage附件,amazon-s3,rails-activestorage,ruby-on-rails-5.2,Amazon S3,Rails Activestorage,Ruby On Rails 5.2,我正在构建一个PDF解析器,它让Sidekiq工作人员从存储在S3中的文档中对数据进行OCR解析。解析后,数据存储在文档模型中 如何将现有S3 bucket文件附加到ActiveStorage中的Document.attachment.attachment,而不复制S3中的文件(通过file.open等…)?创建blob后,只需稍微操纵它即可 storage.yml amazon: service: S3 access_key_id: <%= ENV['AWS_ACCESS_KEY
如何将现有S3 bucket文件附加到ActiveStorage中的
Document.attachment.attachment
,而不复制S3中的文件(通过file.open等…)?创建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'] %>
amazon:
服务:S3
访问密钥id:
机密\u访问\u密钥:
地区:
桶:
app/models/document.rb
class Document < ApplicationRecord
has_one_attached :pdf
end
类文档
rails控制台
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
key=“”
#创建一个表示S3上文件的活动存储blob
参数={
文件名:“myfile.jpg”,
内容类型:“图像/jpeg”,
字节大小:1234,
校验和:“Troy的答案对我来说非常有用!我还发现从对象的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分,所以我将此作为回答而不是评论://我很快阅读了activestorage的源代码,不幸的是我认为这还不可能。我也想使用此功能。此外,这并不是正确的方向。根据您的需要,您可能需要使用File.basename(obj.key)
用于文件名。