1
votes

I have a Rails app which interacts with some mobile apps in various languages via an exposed API.

The mobile apps have to send video files to the Rails app but the size of the files is too large to send them directly to the API to attach with Paperclip.

I am uploading the files directly to S3 on the mobile devices and then sending the URL of the uploaded files to the API using a field which is not associated with a paperclip attachment.

At the point the Rails controller gets an create or update request that contains an upload_url value, I want to create a Paperclip attachment with the file at the location specified.

I know I can create a paperclip attachment by reading the file in from S3 and storing it back out again but this seems like a lot of unnecessary data being transferred when the start and end points are the same bucket on S3.

So my question is, can I somehow use the S3 gem to move or rename the file into being an attached file that Paperclip can recognise?

1

1 Answers

2
votes

It depends on how you are modelling the attachment (like the path and url scheme for s3), but in the most common scenario you can simply create the object with the necessary attributes (including file name) that should be derived from the upload_url value. Then the upload record can be accessed as if it was originally created through Paperclip.

1.9.3p194 :069 > u = Attachment.new
 => #<Attachment id: nil, type: nil, attachable_id: nil, attachable_type: nil, attachment_file_name: nil, attachment_content_type: nil, attachment_file_size: nil, attachment_updated_at: nil, created_at: nil, updated_at: nil> 
1.9.3p194 :070 > u.attachment_file_name = 'fdssfd.txt'
 => "fdssfd.txt" 
1.9.3p194 :071 > u.attachment
 => /system/assets/images/paperclip/development//original/fdssfd.txt 
1.9.3p194 :072 > u.save
   (0.3ms)  BEGIN
  SQL (0.5ms)  INSERT INTO `attachments` (`attachable_id`, `attachable_type`, `attachment_content_type`, `attachment_file_name`, `attachment_file_size`, `attachment_updated_at`, `created_at`, `type`, `updated_at`) VALUES (NULL, NULL, NULL, 'fdssfd.txt', NULL, NULL, '2012-09-18 14:24:26', NULL, '2012-09-18 14:24:26')
[paperclip] Saving attachments.
   (1.4ms)  COMMIT
 => true 
1.9.3p194 :073 > u.attachment
 => /system/assets/images/paperclip/development/5/original/fdssfd.txt