的AWS-S3文檔複製整個水桶的檔案上寫着:如何使用AWS-S3寶石
# Copying an object
S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
但我怎麼從photos
桶複製heashot.jpg
到archive
鬥例如
謝謝!
德布
的AWS-S3文檔複製整個水桶的檔案上寫着:如何使用AWS-S3寶石
# Copying an object
S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
但我怎麼從photos
桶複製heashot.jpg
到archive
鬥例如
謝謝!
德布
使用right_aws
寶石片段:
# 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')
我跑進問題在於,假如你有pics/1234/yourfile.jpg
的bucket
只是pics
和key
是1234/yourfile.jpg
我從這裏得到了答案:How do I copy files between buckets using s3 from a rails application?
aws-s3 gem不具備在文件桶之間複製文件而無需將文件移動到本地計算機的功能。如果這是可以接受你,那麼下面的工作:
AWS::S3::S3Object.store 'dest-key', open('http://url/to/source.file'), 'dest-bucket'
AWS-SDK寶石。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.
它使用copy_object方法internal,所以拷貝功能允許您內或您的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})
請注意AWS S3限制:所有區域的上傳/下載兩個電話的請求數均爲100次/秒。 – Anatoly 2014-04-09 08:57:53
我遇到了同樣的問題,所以我克隆了AWS-S3的源代碼,並創建了一個支持copy_to
方法的分支,該方法允許在桶之間進行復制,而這些桶已被捆綁到我的項目中並在我需要該功能時使用。希望別人也會覺得這很有用。
使用AWS SDK gem的copy_from或copy_to時,默認情況下不會複製三件事:ACL,存儲類或服務器端加密。您需要將它們指定爲選項。
from_object.copy_to from_object.key, {:bucket => 'new-bucket-name', :acl => :public_read}
https://github.com/aws/aws-sdk-ruby/blob/master/lib/aws/s3/s3_object.rb#L904
這裏有一個簡單的Ruby類的所有對象從一個桶複製到另一個桶:https://gist.github.com/edwardsharp/d501af263728eceb361ebba80d7fe324
對於任何人還在找,AWS有documentation這一點。這其實非常簡單與aws-sdk
寶石:
bucket = Aws::S3::Bucket.new('source-bucket')
object = bucket.object('source-key')
object.copy_to(bucket: 'target-bucket', key: 'target-key')
我想使用AWS-S3的片段,但我有初始化AmazoneS3Asset類的問題。我是否簡單地將定義類的代碼片段放在config/initializers中? – deb 2010-08-11 17:38:39
我發現我的評論的答案在這裏http://stackoverflow.com/questions/1146946/ruby-on-rails-and-external-classes – deb 2010-08-11 17:47:54
顯然right_aws有這種功能,我不需要添加片段。我在下面的答案中發佈了更多信息 – deb 2010-08-11 18:24:17