简体   繁体   English

如何使用S3与sdk将整个“文件夹”复制到另一个路径?

[英]How to copy an entire “folder” to another path using S3 with sdk?

When I do for a single file it works: 当我为一个文件做它时,它工作:

    aws_s3 = AWS::S3.new(S3_CONFIG)
    bucket = aws_s3.buckets[S3_CONFIG["bucket"]]

    object = bucket.objects["user/1/photos/image_1.jpg"]
    new_object = bucket.objects["users/1/photos/image_1.jpg"]
    object.copy_to new_object, {:acl => :public_read}

But I want to move the entire "/photos" folder throws No Such Key . 但我想移动整个“/ photos”文件夹抛出No Such Key Probably the s3 keys are only the full path for each file. 可能s3键只是每个文件的完整路径。 How to do that? 怎么做?

    aws_s3 = AWS::S3.new(S3_CONFIG)
    bucket = aws_s3.buckets[S3_CONFIG["bucket"]]

    object = bucket.objects["user/1/photos"]
    new_object = bucket.objects["users/1/photos"]
    object.copy_to new_object, {:acl => :public_read}

Thanks! 谢谢!

Did it: 做到了:

bucket.objects.with_prefix("user/1/photos").each do |object|
   ...
end

I needed additional code to get this working. 我需要额外的代码来实现这一点。 Basically chop off the base from the source prefix, then add that to the destination prefix: 基本上从源前缀中删除基数,然后将其添加到目标前缀:

def copy_files_s3(bucket_name, source, destination)
  source_bucket = @s3.buckets[bucket_name]
  source_bucket.objects.with_prefix(source).each do |source_object|
    new_file_name = source_object.key.dup
    new_file_name.slice! source
    new_object = source_bucket.objects["#{destination}#{new_file_name}"]
    source_object.copy_to new_object, {acl: :public_read}
  end
end

A "folder" is not an object in S3, that is why you can not get it by key, but the folder path is actually a prefix for all the keys of the objects contained by the folder. “文件夹”不是S3中的对象,这就是您无法通过键获取它的原因,但文件夹路径实际上是文件夹所包含对象的所有键的前缀。

Another important thing, you have to url encode the keys otherwise you may end up with an unknown key error. 另一个重要的事情,你必须对密钥进行url编码,否则你可能会得到一个未知的密钥错误。

require 'aws-sdk'
require 'aws-sdk-s3'
require 'securerandom'
require 'uri'
require "erb"
include ERB::Util

def copy_folder(folder, destination)
    bucket_name = 'your_bucket'
    credentials = Aws::Credentials.new('key', 'secret')
    s3_client = Aws::S3::Client.new(region:'the_region', credentials: credentials)
    enumerate_keys_with_prefix(source).each do |source_object|
       source_key = url_encode(source_object.key)
       destination_key = source_object.key.dup.sub(source, "")
       s3_client.copy_object({bucket: bucket_name, copy_source: bucket_name+'/'+source_key, key: destination+'/'+destination_key, acl: "public-read"})
    end
end

def enumerate_keys_with_prefix(prefix)
    bucket_name = 'your_bucket'
    credentials = Aws::Credentials.new('key', 'secret')
    s3 = Aws::S3::Resource.new(region:'the_region', credentials:credentials)
    return s3.bucket(bucket_name).objects(prefix: prefix)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Ruby中的雾复制s3文件夹中的所有文件 - How to copy all files in a folder on s3 using Fog in Ruby 如何在 Ruby 中将所有文件和子文件夹从一个文件夹复制到同一 AWS S3 存储桶中的另一个文件夹 - How do I copy all Files and Sub-Folders from One Folder to another Folder in same AWS S3 bucket in Ruby 如何在 ruby​​ on rails 中使用 AWS-SDK gem 列出 s3 文件夹中的所有文件 - How to list all files in an s3 folder using AWS-SDK gem in ruby on rails 适用于 Ruby 的 AWS S3 开发工具包“多部分复制” - AWS S3 SDK for Ruby 'multipart copy' 创建空文件夹S3 ruby​​ SDK - create empty folder S3 ruby SDK 使用 ruby sdk 将文件夹上传到 s3 - uploading folder to s3 with ruby sdk 使用指定的路径前缀将图像从一个S3存储桶复制到另一个存储桶 - Copy images from one S3 bucket to another with specified path prefixes 如何使用AWS Ruby SDK从具有特定名称的各种子文件夹的S3文件夹中删除超过n天的所有对象 - How to delete all objects from a S3 folder of various sub folder with Specific name which are older than n days using AWS Ruby SDK 使用aws-sdk gem将blob从S3复制到Swift - Copy blob from S3 to Swift using aws-sdk gem 如何使用 AWS SDK 从 S3 下载对象 - RESTful - How to download objects from S3 using AWS SDK - RESTful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM