How to check if a resource exists in an AWS S3buck

2019-02-24 00:09发布

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.

标签: ruby aws-sdk
1条回答
2楼-- · 2019-02-24 00:40

#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

查看更多
登录 后发表回答