2010-08-11 54 views
12

的AWS-S3文檔複製整個水桶的檔案上寫着:如何使用AWS-S3寶石

# Copying an object 
    S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos' 

但我怎麼從photos桶複製heashot.jpgarchive鬥例如

謝謝!

德布

回答

6

使用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.jpgbucket只是picskey1234/yourfile.jpg

我從這裏得到了答案:How do I copy files between buckets using s3 from a rails application?

0

我相信,爲了桶之間進行復制,你必須從源頭桶讀取文件的內容,然後將其寫回到通過您的應用程序的內存空間目的地桶。有顯示這個使用aws-s3here和另一種方法使用right_awshere

+0

我想使用AWS-S3的片段,但我有初始化AmazoneS3Asset類的問題。我是否簡單地將定義類的代碼片段放在config/initializers中? – deb 2010-08-11 17:38:39

+0

我發現我的評論的答案在這裏http://stackoverflow.com/questions/1146946/ruby-on-rails-and-external-classes – deb 2010-08-11 17:47:54

+0

顯然right_aws有這種功能,我不需要添加片段。我在下面的答案中發佈了更多信息 – deb 2010-08-11 18:24:17

0

aws-s3 gem不具備在文件桶之間複製文件而無需將文件移動到本地計算機的功能。如果這是可以接受你,那麼下面的工作:

AWS::S3::S3Object.store 'dest-key', open('http://url/to/source.file'), 'dest-bucket' 
25

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桶之間複製對象,並且任選以取代在過程對象相關聯的元數據。

標準方法(下載/上傳)

enter image description here

複印方法

enter image description here

代碼示例:

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}) 
+1

請注意AWS S3限制:所有區域的上傳/下載兩個電話的請求數均爲100次/秒。 – Anatoly 2014-04-09 08:57:53

0

我遇到了同樣的問題,所以我克隆了AWS-S3的源代碼,並創建了一個支持copy_to方法的分支,該方法允許在桶之間進行復制,而這些桶已被捆綁到我的項目中並在我需要該功能時使用。希望別人也會覺得這很有用。

View the branch on GitHub

0

對於任何人還在找,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')