简体   繁体   English

使用aws-sdk将存储桶中的文件复制到回形针附件

[英]Copy file in a bucket to a paperclip attachment with aws-sdk

I have a file in aws S3 that I want to copy to an active record paperclip attachment (as a copy of the file in a different bucket). 我在aws S3中有一个文件,想要复制到活动记录回形针附件中(作为其他存储桶中文件的副本)。

I have a reference to the object on S3 using the aws-sdk gem: 我使用aws-sdk gem引用了S3上的对象:

s3 = AWS::S3.new(Rails.application.config.s3_creds)
obj = s3.buckets(uploads_bucket).objects.first

Say my active record model my_model has 说我的活动记录模型my_model

has_attached_file :some_file

How do I copy obj into my_model.some_file ? 如何将obj复制到my_model.some_file


my_model.some_file = obj throws: No handler found for AWS::S3::S3Object:bucket/the_file.xls my_model.some_file = obj throws:未找到适用于AWS :: S3 :: S3Object:bucket / the_file.xls的处理程序

obj.copy_to(my_model.some_file) throws undefined method `scan' for /file_name/original/missing.png:Paperclip::Attachment obj.copy_to(my_model.some_file)为/file_name/original/missing.png:Paperclip::Attachment抛出未定义的方法“扫描”

I'm copying the file using AWS::S3::S3Object#copy_to(target_obj) and I get target obj by calling #s3_object() on the attachment after setting some column values and saving it: 我使用AWS :: S3 :: S3Object#copy_to(target_obj)复制文件,并在设置一些列值并保存后通过在附件上调用#s3_object()来获取目标obj:

my_model.some_file_file_name = obj.key
my_model.some_file_file_size = obj.content_length
my_model.some_file_content_type = obj.content_type
my_model.some_file_updated_at = obj.last_modified
my_model.save

obj.copy_to(my_model.some_file.s3_object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM