deb*_*deb 12 ruby gem ruby-on-rails amazon-s3
aws-s3文档说:
# Copying an object
S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
Run Code Online (Sandbox Code Playgroud)
但是我如何heashot.jpg从photos桶中复制到archive桶中
谢谢!
德布
Ana*_*oly 26
AWS-SDK gem. S3Object#copy_to
Copies data from the current object to another object in S3.
S3 handles the copy so the client does not need to fetch the
data and upload it again. You can also change the storage
class and metadata of the object when copying.
Run Code Online (Sandbox Code Playgroud)
它使用copy_object方法内部,因此复制功能允许您复制S3存储桶内或之间的对象,并可选择替换与进程中的对象关联的元数据.
标准方法(下载/上传)

复制方法

代码示例:
require 'aws-sdk'
AWS.config(
:access_key_id => '***',
:secret_access_key => '***',
:max_retries => 10
)
file = 'test_file.rb'
bucket_0 = {:name => 'bucket_from', :endpoint => 's3-eu-west-1.amazonaws.com'}
bucket_1 = {:name => 'bucket_to', :endpoint => 's3.amazonaws.com'}
s3_interface_from = AWS::S3.new(:s3_endpoint => bucket_0[:endpoint])
bucket_from = s3_interface_from.buckets[bucket_0[:name]]
bucket_from.objects[file].write(open(file))
s3_interface_to = AWS::S3.new(:s3_endpoint => bucket_1[:endpoint])
bucket_to = s3_interface_to.buckets[bucket_1[:name]]
bucket_to.objects[file].copy_from(file, {:bucket => bucket_from})
Run Code Online (Sandbox Code Playgroud)
使用right_awsgem:
# With s3 being an S3 object acquired via S3Interface.new
# Copies key1 from bucket b1 to key1_copy in bucket b2:
s3.copy('b1', 'key1', 'b2', 'key1_copy')
Run Code Online (Sandbox Code Playgroud)
我跑进问题在于,假如你pics/1234/yourfile.jpg的bucket仅仅是pics和key是1234/yourfile.jpg
我从这里得到了答案:如何使用rails应用程序中的s3在存储桶之间复制文件?
对于仍在寻找的人,AWS 有这方面的文档。实际上,使用aws-sdkgem 非常简单:
bucket = Aws::S3::Bucket.new('source-bucket')
object = bucket.object('source-key')
object.copy_to(bucket: 'target-bucket', key: 'target-key')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10954 次 |
| 最近记录: |