gpt4 book ai didi

ruby - 检查 s3 存储桶中是否存在文件夹

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:00 24 4
gpt4 key购买 nike

如何使用 Ruby on Rails 检查我的 s3 存储桶中是否存在某个文件夹?

我正在使用 AWS:S3 官方 gem初始化全局连接后

AWS::S3::Base.establish_connection!(:access_key_id => 'my_key_id', :secret_access_key => ‘my_secret’) 

我的桶名为:myfirstbucket

内部文件夹名为:my_folder

my_folder 中的文件名为:my_pic.jpg

当我尝试检查 my_pic.jpg 是否存在时,它工作得很好

s3object.exists? “/my_folder/my_pic.jpg” , “myfirstbucket”
=> True

如何只检查文件夹是否存在?

s3object = AWS::S3::S3Object
s3object.exists? “/my_folder/” , “myfirstbucket”
=> False

最佳答案

使用Bucket#objects:

bucket.objects({prefix: 'my/folder/'}).limit(1).any?

Returns a Collection of ObjectSummary resources. No API requests are made until you call an enumerable method on the collection. Client#list_objects will be called multiple times until every ObjectSummary has been yielded.

http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Bucket.html#objects-instance_method

关于ruby - 检查 s3 存储桶中是否存在文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com