如何检查AWS S3bucket中是否存在资源

时间:2015-10-12 07:30:34

标签: ruby aws-sdk

我有一个AWS S3存储桶,我有多个文件夹。

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

假设我有一个名为demo/index.html的资源,我将如何检查this bucket中是否存在此资源?

可能我的问题太简单了,但我无法为此找到合适的答案。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:7)

<强> #exists? ⇒布尔

  

如果对象存在于S3中,则返回true。

# 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

相关问题