ruby-on-rails-3amazon-web-servicesamazon-s3amazon-rekognitionaws-sdk-ruby

Fetch an AWS S3 object to use in Rekognition when uploaded via Carrierwave


I have a Gallery and Attachment models. A gallery has_many attachments and essentially all attachments are images referenced in the ':content' attribute of Attachment.

The images are uploaded using Carrierwave gem and are stored in Aws S3 via fog-aws gem. This works OK. However, I'd like to conduct image recognition to the uploaded images with Amazon Rekognition.

I've installed aws-sdk gem and I'm able to instantiate Rekognition without a problem until I call the detect_labels method at which point I have been unable to use my attached images as arguments of this method.

So fat I've tried:

@attachement = Attachment.first
client = Aws::Rekognition::Client.new
resp = client.detect_labels(
         image: @attachment
       )
# I GET expected params[:image] to be a hash... and got class 'Attachment' instead

I've tried using:

client.detect_labels( image: { @attachment })
client.detect_labels( image: { @attachment.content.url })
client.detect_labels( image: { @attachment.content })

All with the same error. I wonder how can I fetch the s3 object form @attachment and, even if I could do that, how could I use it as an argument in detect_labels.

I've tried also fetching directly the s3 object to try this last bit:

s3 = AWS:S3:Client.new
s3_object = s3.list_objects(bucket: 'my-bucket-name').contents[0]

# and then

client.detect_labels( image: { s3_object })

Still no success...

Any tips?


Solution

  • I finally figured out what was the problem, helped by the following AWS forum

    The 'Image' hash key takes as a value an object that must be named 's3_object' and which subsequently needs only the S3 bucket name and the path of the file to be processed. As a reference see the correct example below:

    client = Aws::Rekognition::Client.new
    resp = client.detect_labels(
             image:
                { s3_object: {
                  bucket: "my-bucket-name",
                  name: @attachment.content.path, 
                },
              }
           )
    
    # @attachment.content.path => "uploads/my_picture.jpg"