简体   繁体   English

使用Ruby aws-sdk选择并使用AWS s3对象密钥的一部分

[英]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.

相关问题 如何在Ruby中对S3和AWS-SDK使用经过身份验证的用户 - How to use an authenticated user with S3 and AWS-SDK in Ruby Ruby AWS :: S3 :: S3Object(aws-sdk):是否有与aws-s3一样的流数据传输方法? - Ruby AWS::S3::S3Object (aws-sdk): Is there a method for streaming data as there is with aws-s3? 无法使用 aws-sdk ruby​​ gem 创建存储桶。 Aws::S3::Errors::SignatureDoesNotMatch - Can't create bucket using aws-sdk ruby gem. Aws::S3::Errors::SignatureDoesNotMatch AWS S3红宝石AWS-SDK文件传输器使用关键云铸造 - aws s3 ruby aws-sdk file transer using pivotal cloud foundry 使用aws-sdk ruby​​ gem检查AWS S3中是否存在新创建的文件 - Checking if a newly created file exists in AWS S3 using aws-sdk ruby gem 使用aws-sdk - > 2和ruby更改s3文件访问权限 - Change s3 file access with aws-sdk -> 2 and ruby 适用于Ruby v2的aws-sdk:在我将对象放入S3存储桶中后检查成功状态 - aws-sdk for Ruby v2: check success status after I PUT object in S3 bucket 有没有一种方法可以使用mv命令通过aws-sdk ruby​​ gem在S3存储桶中移动目录? - Is there a way to use mv command to move directory in S3 bucket with aws-sdk ruby gem? Ruby:使用aws-sdk列出带有marker和max-keys的s3对象 - Ruby: use aws-sdk to list s3 objects with marker and max-keys 无法使用ruby aws-sdk v2在S3中创建存储桶 - Cannot create a bucket in S3 using ruby aws-sdk v2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM