2013-04-29 45 views
2

我想在aws S3中將文件複製到活動記錄回形針附件(作爲不同存儲桶中文件的副本)。使用aws-sdk將存儲桶中的文件複製到回形針附件

我必須使用aws-sdk創業板S3的參考對象:

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

說我my_model

has_attached_file :some_file 

的活動記錄模式如何複製objmy_model.some_file


my_model.some_file = obj拋出: 沒有找到處理程序爲 AWS :: S3 :: S3Object:桶/ the_file.xls

obj.copy_to(my_model.some_file)拋出未定義的方法'掃描」用於/ FILE_NAME /原廠/失蹤.png:回形針::附件

回答

0

我使用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) 
相關問題