如何检查AWS S3bucket中是否存在资源 [英] How to check if a resource exists in an AWS S3bucket

查看:980
本文介绍了如何检查AWS S3bucket中是否存在资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AWS S3存储桶,其中有多个文件夹.

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

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

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

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.

推荐答案

#exists? ⇒布尔值

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

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

这篇关于如何检查AWS S3bucket中是否存在资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆