Это можно сделать с помощью небольших манипуляций с большим двоичным объектом после его создания.
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
end
rails console
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
На данный момент вы можете использовать атрибут pdf
вашего объекта Document
как и любое другое активное хранилище.