Ste-3PO
Ste-3PO

Reputation: 1305

Select and use part of an AWS s3 object key using Ruby aws-sdk

I am trying to list only the objects from the s3 folder (not a real folder I know) called distribution but I want to remove the reference to the name and any slashes around the object. The output should just look like 021498cd-ca73-4675-a57a-c12b3c652aac whereas currently it looks like distribution/021498cd-ca73-4675-a57a-c12b3c652aac/

So far I have tried;

def files
    s3 = Aws::S3::Resource.new


    s3.client
    bucket = s3.bucket('test')
    files = []

    bucket.objects.each do |obj|
     if obj.key.include?('distribution/')
      temp_files = puts "#{obj.key}"
      files = temp_files.select do |file|
        file.gsub("distribution/", "")
      end
    else
    end
 end
end

But this doesn't seem to be working at all.

Upvotes: 0

Views: 295

Answers (1)

Noman Ur Rehman
Noman Ur Rehman

Reputation: 6957

Your explanation is pretty simple but your code is implying something else.

However, this should help with what you are trying to achieve.

def files
  s3 = Aws::S3::Resource.new

  s3.client
  bucket = s3.bucket('test')
  files = []

  bucket.objects.each do |obj|
    if obj.key.include?('distribution/')
      files << "#{file.gsub(/(distribution)|\//, '')}"
    end   
  end
end

The files array will contain all the file names with garbage stripped.

Upvotes: 1

Related Questions