2014-03-13 88 views
1

我想將我的資產文件夾移動到Amazon S3,並且由於它的尺寸很大,在交易過程中我需要通過回形針上傳本地存儲和亞馬遜s3文件。回形針多個存儲

有沒有辦法配置回形針來存儲文件系統和亞馬遜s3上傳的文件?

回答

1

也許你會受益於此:

http://airbladesoftware.com/notes/asynchronous-s3/

你必須做的是先上傳到自己的本地存儲,然後在「異步」上傳到S3

這通常是通過ResqueDelayedJob(如本教程演示的)完成的,並且將要求您在服務器上運行某種第三方處理引擎(通常爲Re DIS或類似的)

從教程:

### Models ### 

class Person < ActiveRecord::Base 
    has_attached_file :local_image, 
        path: ":rails_root/public/system/:attachment/:id/:style/:basename.:extension", 
        url: "/system/:attachment/:id/:style/:basename.:extension" 

    has_attached_file :image, 
        styles: {large: '500x500#', medium: '200x200#', small: '70x70#'}, 
        convert_options: {all: '-strip'}, 
        storage:   :s3, 
        s3_credentials: "#{Rails.root}/config/s3.yml", 
        s3_permissions: :private, 
        s3_host_name: 's3-eu-west-1.amazonaws.com', 
        s3_headers:  {'Expires'    => 1.year.from_now.httpdate, 
             'Content-Disposition' => 'attachment'}, 
        path:   "images/:id/:style/:filename" 

    after_save :queue_upload_to_s3 

    def queue_upload_to_s3 
    Delayed::Job.enqueue ImageJob.new(id) if local_image? && local_image_updated_at_changed? 
    end 

    def upload_to_s3 
    self.image = local_image.to_file 
    save! 
    end 
end 

class ImageJob < Struct.new(:image_id) 
    def perform 
    image = Image.find image_id 
    image.upload_to_s3 
    image.local_image.destroy 
    end 
end 

### Views ### 

# app/views/people/edit.html.haml 
# ... 
= f.file_field :local_image 

# app/views/people/show.html.haml 
- if @person.image? 
    = image_tag @person.image.expiring_url(20, :small) 
- else 
    = image_tag @person.local_image.url, size: '70x70'