[英]Select and use part of an AWS s3 object key using Ruby aws-sdk
I am trying to list only the objects from the s3 folder (not a real folder I know) called distribution
but I want to remove the reference to the name and any slashes around the object. 我试图只列出s3文件夹(不是我知道的真实文件夹)中的对象,称为
distribution
但我想删除对该名称的引用以及该对象周围的任何斜杠。 The output should just look like 021498cd-ca73-4675-a57a-c12b3c652aac
whereas currently it looks like distribution/021498cd-ca73-4675-a57a-c12b3c652aac/
输出应该看起来像
021498cd-ca73-4675-a57a-c12b3c652aac
而当前看起来像是distribution/021498cd-ca73-4675-a57a-c12b3c652aac/
So far I have tried; 到目前为止,我已经尝试过;
def files
s3 = Aws::S3::Resource.new
s3.client
bucket = s3.bucket('test')
files = []
bucket.objects.each do |obj|
if obj.key.include?('distribution/')
temp_files = puts "#{obj.key}"
files = temp_files.select do |file|
file.gsub("distribution/", "")
end
else
end
end
end
But this doesn't seem to be working at all. 但这似乎根本不起作用。
Your explanation is pretty simple but your code is implying something else. 您的解释非常简单,但是您的代码暗示了其他内容。
However, this should help with what you are trying to achieve. 但是,这应该可以帮助您实现目标。
def files
s3 = Aws::S3::Resource.new
s3.client
bucket = s3.bucket('test')
files = []
bucket.objects.each do |obj|
if obj.key.include?('distribution/')
files << "#{file.gsub(/(distribution)|\//, '')}"
end
end
end
The files
array will contain all the file names with garbage stripped. files
数组将包含所有文件名,并去除了垃圾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.