Sha256: ab7b16153a2c7b1d5c6ceb94f6d3ded825d2071990fdcb67bbe6555dcdeb7bda

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'aliyun/oss'

module Backup
  module Storage
    class Aliyun < Base
      attr_accessor :bucket, :area, :access_key_id, :access_key_secret, :path

      def initialize(model, storage_id = nil, &block)
        super(model, storage_id)

        @path ||= 'backups'
        @area ||= 'cn-hangzhou'

        instance_eval(&block) if block_given?
      end

      private

      def client
        return @client if defined? @client
        opts = {
          host: "oss-#{self.area}.aliyuncs.com",
          bucket: self.bucket
        }
        @client = ::Aliyun::Oss::Client.new(self.access_key_id, self.access_key_secret, opts)
        @client
      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|
            client.bucket_create_object(dest, file, {})
          end
        end
      end

      def remove!(package)
        remote_path = remote_path_for(package)
        Logger.info "#{storage_name} removing '#{remote_path}'..."
        client.bucket_delete_object(remote_path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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