如何在rails上的ruby中使用AWS-SDK gem列出s3文件夹中的所有文件

时间:2022-12-16 10:41:37

I wanted to show a list of all files in an s3 folder so I can get all the last modified dates so I can determine what files has been changed.

我想显示s3文件夹中所有文件的列表,以便我可以获取所有上次修改日期,以便我可以确定哪些文件已被更改。

I tried using objects.with_prefix('Folder1') it give me a full list but also contain Folder1.1 key

我尝试使用objects.with_prefix('Folder1')它给我一个完整的列表,但也包含Folder1.1键

I don't know if i needed to use delimiter but I couldn't find anything how to use delimiter in aws sdk.

我不知道我是否需要使用分隔符,但我找不到任何如何在aws sdk中使用分隔符。

Thanks so much in advance!

非常感谢提前!

I'm using 'aws-sdk' gem

我正在使用'aws-sdk'宝石

Here is my bucket structure -Folder1 -File1 -File2 -Folder.1.1

这是我的桶结构-Folder1 -File1 -File2 -Folder.1.1

Here is my code

这是我的代码

bucket = s3.buckets[bucket_name]
data = bucket.objects.with_prefix('Folder1/')
data.each do |object|
    puts "#{object.key}\t#{object.last_modified}";
end

4 个解决方案

#1


26  

Too late answer but better than never.

答案太晚了,但总比没有好。

You can do

你可以做

s3_bucket.objects.with_prefix('folder_name').collect(&:key)

According to official documentation here

根据这里的官方文件

#2


9  

You can use this small piece of code for getting list of files for a specific folder.

您可以使用这一小段代码获取特定文件夹的文件列表。

 s3 = Aws::S3::Resource.new(region: 'ap-southeast-1', access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] )
 data_files = s3.bucket(bucket_name).objects(prefix: 'prefix/', delimiter: 'delimiter').collect(&:key)

#3


2  

Currently I am also stuck with this problem. So far solution is to fetch all the objects and to filter them later:

目前我也遇到了这个问题。到目前为止,解决方案是获取所有对象并在以后过滤它们:

data = bucket.objects(bucketname, prefix: 'Folder1')

data_without_folders = data.select { |obj| !(obj.key =~ /\/$/) }

For delimiter, you just have to pass it in bucket.objects call like:

对于分隔符,你只需要在bucket.objects调用中传递它:

data = bucket.objects(bucketname, prefix: 'prefix', delimiter: 'delimiter') 

If better solution is available, I will let you know.

如果有更好的解决方案,我会通知你。

#4


1  

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#list_objects_v2-instance_method

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#list_objects_v2-instance_method

SDK V3 has the prefix option for client!

SDK V3具有客户端的前缀选项!

resp = client.list_objects_v2({ bucket: "BucketName", # required prefix: "FolderName", })

resp = client.list_objects_v2({bucket:“BucketName”,#required prefix:“FolderName”,})

#1


26  

Too late answer but better than never.

答案太晚了,但总比没有好。

You can do

你可以做

s3_bucket.objects.with_prefix('folder_name').collect(&:key)

According to official documentation here

根据这里的官方文件

#2


9  

You can use this small piece of code for getting list of files for a specific folder.

您可以使用这一小段代码获取特定文件夹的文件列表。

 s3 = Aws::S3::Resource.new(region: 'ap-southeast-1', access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] )
 data_files = s3.bucket(bucket_name).objects(prefix: 'prefix/', delimiter: 'delimiter').collect(&:key)

#3


2  

Currently I am also stuck with this problem. So far solution is to fetch all the objects and to filter them later:

目前我也遇到了这个问题。到目前为止,解决方案是获取所有对象并在以后过滤它们:

data = bucket.objects(bucketname, prefix: 'Folder1')

data_without_folders = data.select { |obj| !(obj.key =~ /\/$/) }

For delimiter, you just have to pass it in bucket.objects call like:

对于分隔符,你只需要在bucket.objects调用中传递它:

data = bucket.objects(bucketname, prefix: 'prefix', delimiter: 'delimiter') 

If better solution is available, I will let you know.

如果有更好的解决方案,我会通知你。

#4


1  

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#list_objects_v2-instance_method

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#list_objects_v2-instance_method

SDK V3 has the prefix option for client!

SDK V3具有客户端的前缀选项!

resp = client.list_objects_v2({ bucket: "BucketName", # required prefix: "FolderName", })

resp = client.list_objects_v2({bucket:“BucketName”,#required prefix:“FolderName”,})