Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a resource exists in an AWS S3bucket

Tags:

ruby

aws-sdk

I have an AWS S3 bucket to which I have multiple folders.

s3 = AWS::S3.new
bucket =  s3.buckets['test']
bucket.exists? => true

Say I have a resource named demo/index.html, how I will check whether this resource is present in this bucket?

May be my question is too simple, but I am not able to find a proper answer for this. Any help is appreciated.

like image 793
ejo Avatar asked Oct 12 '15 07:10

ejo


People also ask

Can you query an S3 bucket?

Amazon S3 Select and Amazon S3 Glacier Select enable customers to run structured query language SQL queries directly on data stored in S3 and Amazon S3 Glacier. With S3 Select, you simply store your data on S3 and query using SQL statements to filter the contents of S3 objects, retrieving only the data that you need.

How do I view contents of S3 bucket?

To open the overview pane for an objectSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview.


1 Answers

#exists? ⇒ Boolean

Returns true if the object exists in S3.

# new object, does not exist yet
obj = bucket.objects["my-text-object"]
# no instruction file present
begin
  bucket.objects['my-text-object.instruction'].exists? #=> false
rescue
  # exists? can raise an error `Aws::S3::Errors::Forbidden`
end


# store the encryption materials in the instruction file
# instead of obj#metadata
obj.write("MY TEXT",
  :encryption_key => MY_KEY,
  :encryption_materials_location => :instruction_file)

begin
  bucket.objects['my-text-object.instruction'].exists? #=> true
rescue
  # exists? can raise an error `Aws::S3::Errors::Forbidden`
end    

http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#exists%3F-instance_method

like image 55
Arsen Avatar answered Sep 30 '22 03:09

Arsen