views:

96

answers:

1

Hi Guys, I'm having few doubts on implementing file downloads. I'm creating an app where I use attachment_fu with Amazon s3 to upload files. Things are working pretty well so far on uploading side. Now its the time to start the file downloads. Here is what I need, a logged in user search and browse for Images and they should able to add the files in to a download basket (Let's say its a Download Shopping Cart). Finally the user should be able to download these file(s) from S3 probably as a zipped file.

Is there any plugin/gem where I can use for this?

+1  A: 

The downside of giving the customer a zip file of all the files is that you'll need to first pull all of the files from S3 back onto your server, then zip them.

You can certainly do that if you want, but it will take a bit of time, you would not want to do it synchronously as part of the browser request. Instead, do it as a background job using delayed_job or similar.

To do the actual zipping, use Zlib::GzipWriter See http://ruby-doc.org/core/classes/Zlib/GzipWriter.html -- it is part of standard Ruby

You could then:

  1. email the user the actual zip file as an attachment
  2. email the user the link to the zip file on your server
  3. or upload the zip file to s3, then email a link to the zip file on s3

Remember to create a clean up task/job to remove the old zip files from your system...

Alternative is to not zip the files together, instead, give the user one or more links to download the files separately.

S3 enables you to create a url to an S3 file that can be used for a set period of time. (The file would be private on S3 so a straight link to it won't work.) Here's how to create it using attachment-fu and aws-s3 gem:

# I added this as a method to my model for the files stored in S3
def authenticated_s3_url
  # return a publicly usable url
  connect_to_aws # a local method which connects/re-connects to s3
  S3Object.url_for(full_filename, 
             bucket_name, 
            :expires_in => 60 * 60) # 1 hour
end
Larry K
Thanks Larry K.I understand that zipping is not a good idea and best mechanism is providing direct links to files. Anyway if I do this, users won't be able to download batch of files at once unless I upload zipped file back to S3. I'm not that much familiar with S3 yet, so that first I'll try the direct linking as you suggested. I hope what I need here is "Signed URL" right?
randika
If you store the files on S3 without public access permissions, then yes, you'll need to supply a signed url to your users to enable them to download the files.Also, zipping files is a fine idea if that's what you want to do--especially if most of your customers will be receiving more than 3 files. Below 3 files, the bother of unzipping the zip archive is probably not worth it.ps. Remember to give credit to stack overflow answers which are of help to you.
Larry K