JoHksi JoHksi - 1 year ago 86
Ruby Question

Retrieving files from AWS S3 in Ruby

I uploaded multiple PDF files in following path (

) in AWS S3. So that path for each file is going to be like
, etc.

In my website(Angular front-end and Rails backend), I'm trying to do 3 things.
1) Retrieving files in certain path (
2) Make a view which lists names of the files I retrieved from certain path.
3) Let users to click the name of the file and it will open the file using S3 endpoint
4) Delete the file by clicking a button.

I was looking into AWS S3 doc, but I could not find related API calls from the doc. Would love to get some help on performing above actions.

Answer Source

you should review the ruby S3 sdk doc

  1. listing objects from a bucket

    # enumerate ALL objects in the bucket (even if the bucket contains
    # more than 1k objects)
    bucket.objects.each do |obj|
      puts obj.key
    # enumerate at most 20 objects with the given prefix
    bucket.objects.with_prefix('photos/').each(:limit => 20) do |photo|
      puts photo.key
  2. getting an object

    # makes no request, returns an AWS::S3::S3Object
    obj = bucket.objects['key']
  3. deleting an object

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download