简体   繁体   中英

Checking if a newly created file exists in AWS S3 using aws-sdk ruby gem

I am using aws-sdk ruby gem (v 1.66.0) to upload a file to S3

bucket = AWS::S3.new.buckets[@bucket_name] obj = bucket.objects['/path/to/file_to_upload.pdf'] obj.write(:file => "/file_to_upload.pdf").exists? # => false

Even though the file "file_to_upload.pdf" does get uploaded to the designated path, calling .exists? on the object returns false.

This only happened on the production environment. Running the same code in our QA environment returned true . I am assuming it is some kind of permissions issue, but I couldn't find an answer how to change this. Any ideas?

After researching for a while, it seems that ObjectVersioning was turned on in AWS S3 for the production bucket. When ObjectVersioning is turned on, calling obj.write(:file => "/file_to_upload.pdf") returns ObjectVersion, which does not have .exists? method defined. When ObjectVersioning is turned off, obj.write(:file => "/file_to_upload.pdf") returns Object which has .exists? method.

Weird and inconsistent but I hope this helps for anyone else out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM