Galet
Galet

Reputation: 6269

How to verify AWS S3 put response in Rails

I am using aws-skd-s3 gem in my Rails project.

Create S3 resoure

s3 = Aws::S3::Resource.new(access_key_id: #####,
        secret_access_key: #####,
        region: 'us-east-1')

Create an S3 object

path = 'sample'
key = test.csv
obj = s3.bucket(#{bucket_name}).object("#{path}" + key)

Store CSV in S3

obj.put(body: csv_response, content_type: 'text/csv')

How to verify that put method stored the csv in S3 without any issues?

Is there any status code available for put method in S3 to verify?

Upvotes: 0

Views: 628

Answers (2)

Vaibhav
Vaibhav

Reputation: 658

Two ways to go about it:

  1. Store the result. It should be a PutObjectOutput type object. You can check out the official method documentation of the put request method.
  2. The second way to go about it is to make a exists? call right after your put request is completed. Something like this:
   s3 = Aws::S3::Resource.new(region: 'ap-southeast-1') # change to the region you use
   obj = s3.bucket('bucket-name').object("path/to/object/in/bucket")
   if obj.exists?
     # Object was uploaded successfully!
   else
     # No it wasn't!
   end

Hope that helps!

Upvotes: 1

Subash
Subash

Reputation: 3168

One way I've seen or read other people doing it is calculating a md5 hash of the original file before upload and then match that with the etag value from the response of obj.put

Upvotes: 1

Related Questions