Sha256: 0af0623766fea07dc9a95842130dd0525f1598b38793f8067b800d17ef39f523

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require "carrierwave-aliyun"
require "base64"

module Backup
  module Storage
    class Aliyun < Base
      attr_accessor :bucket,:access_key_id,:access_key_secret,:aliyun_internal, :path
      
      def initialize(model, storage_id = nil, &block)
        super(model, storage_id)

        @path ||= 'backups'

        instance_eval(&block) if block_given?
      end
      
      private
      
      def connection
        return @connection if @connection
        opts = {
          :aliyun_access_id => self.access_key_id,
          :aliyun_access_key => self.access_key_secret, 
          :aliyun_bucket => self.bucket,
          :aliyun_internal => self.aliyun_internal || false
        }
        @connection = CarrierWave::Storage::Aliyun::Connection.new(opts)
      end
      
      def transfer!
        remote_path = remote_path_for(@package)

        @package.filenames.each do |filename|
          src = File.join(Config.tmp_path, filename)
          dest = File.join(remote_path, filename)
          Logger.info "#{storage_name} uploading '#{ dest }'..."
          File.open(src, 'r') do |file|
            connection.put(dest, file.read)
          end
        end
      end
      
      def remove!(package)
        remote_path = remote_path_for(package)
        Logger.info "#{storage_name} removing '#{remote_path}'..."
        connection.delete(remote_path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backup-aliyun-0.1.0 lib/backup/storage/aliyun.rb