JoHksi JoHksi - 4 months ago 10
Ruby Question

Retrieving files from AWS S3 in Ruby

I uploaded multiple PDF files in following path (

user/pdf/
) in AWS S3. So that path for each file is going to be like
user/pdf/file1.pdf
,
user/pdf/file2.pdf
, etc.

In my website(Angular front-end and Rails backend), I'm trying to do 3 things.
1) Retrieving files in certain path (
user/pdf/
).
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

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
    end
    
    # enumerate at most 20 objects with the given prefix
    bucket.objects.with_prefix('photos/').each(:limit => 20) do |photo|
      puts photo.key
    end
    
  2. getting an object

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

    bucket.objects.delete('abc')
    
Comments