MEH
MEH

Reputation: 1864

How to get objects' keys in amazon s3 in Rails?

I use S3 to store some photos to send their path through rails to mobile clients, but when I started to send the data for a specific item in show method I used this code with aws-sdk gem to check where is the photos and get their path

s3 = Aws::S3::Resource.new(
      access_key_id: 'askdlkasdmkmakml',
      secret_access_key: 'aklsdmkmasldkmasmdlmasdl',
      region: 'ap-southeast-1'
    )

    images = []
    s3.bucket('my-pics').objects.find_all do |object| 
        images << object.key if object.key.include?(self.barcode_number})
    end

this code I tested it with ruby not rails and I get this as result:

photos/9000101044393/1.jpg photos/9000101044393/2.jpg

but when I use this code inside rails I got all these stuff

[#<Aws::S3::ObjectSummary:0x007fe348713170 @bucket_name="my-pics",     
@key="photos/9000101044393/1.jpg", @data=#<struct     
Aws::S3::Types::Object key="photos/9000101044393/1.jpg",     
last_modified=2018-03-01 05:14:57 UTC,     
etag="\"ee4540acc2a5bfc948507e0927e9dd1b\"", size=140119,     
storage_class="STANDARD", owner=#<struct Aws::S3::Types::Owner 
display_name="mohammed.eliass",     
id="06bf2e3c37f83d96de16b13fc00efc20a097988edd1d4">>, @client=#    
<Aws::S3::Client>>, #<Aws::S3::ObjectSummary:0x007fe348713008     
@bucket_name="my-pics", @key="photos/9000101044393/2.jpg", @data=#   
<struct Aws::S3::Types::Object key="photos/9000101044393/2.jpg",    
last_modified=2018-03-01 05:14:57 UTC,    
etag="\"b13dc7b1a516fee5ed15bccc57e\"", size=33132,    
storage_class="STANDARD", owner=#<struct Aws::S3::Types::Owner    
display_name="mohammed.eliass",  id="06bf2e3c37f83d96de16b13fc0adasdaddskfl88edd1d449d85b7157d95bdf334">>,

@client=#<Aws::S3::Client>>, #<Aws::S3::ObjectSummary:0x007fe348712ef0     
@bucket_name="my-pics", @key="photos/9000101044430/1.jpg", @data=#    
<struct Aws::S3::Types::Object key="photos/9000101044430/1.jpg",     
last_modified=2018-03-01 05:14:59 UTC,     
etag="\"308ab75c98257b821469b4b93e9ca8f8\"", size=141066,    
storage_class="STANDARD", owner=#<struct Aws::S3::Types::Owner    
display_name="mohammed.eliass"]

So any ideas how should I get rid of all this and get only the keys?

Upvotes: 0

Views: 2835

Answers (2)

Ben
Ben

Reputation: 5172

From the docs :

bucket.objects.each do |obj|
  puts obj.key
end

Untested, but if you just just want the matching keys in your output :

def matching_s3_keys
  s3 = Aws::S3::Resource.new(
    access_key_id: 'askdlkasdmkmakml',
    secret_access_key: 'aklsdmkmasldkmasmdlmasdl',
    region: 'ap-southeast-1'
  )
  images = s3.bucket('my-pics').objects.select do |object| 
    # This looks a bit weird has i'm unsure if you really
    # want to match a s3key with another value in your app/db.
    # Just showing here a way to filter your results
    #
    object.key == self.barcode_number
  end
  images.map(&:key)
end

Upvotes: 2

Simon Franzen
Simon Franzen

Reputation: 2727

Try this to retrieve the object keys

s3 = AWS::S3.new

s3.buckets['bucket-name'].objects.each do |o|
   puts o.key
end

https://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html


In your case

images = []
s3.buckets['my-pics'].objects.each do |object| 
    images << object.key if object.key.include?(self.barcode_number})
end

Upvotes: 1

Related Questions