Sha256: 5e90a9e15afd582a8b28f2c86ff4ab637df07feb010a5d5891aafa0519e108dc

Contents?: true

Size: 1.68 KB

Versions: 26

Compression:

Stored size: 1.68 KB

Contents

# encoding: utf-8
require 'qiniu'

module Backup
  module Storage
    class Qiniu < Base
      include Storage::Cycler
      class Error < Backup::Error; end

      ##
      # Qiniu API credentials
      attr_accessor :access_key, :secret_key

      ##
      # Qiniu bucket name
      attr_accessor :bucket

      def initialize(model, storage_id = nil)
        super

        @path ||= 'backups'

        check_configuration
        config_credentials
      end

      private
      def transfer!
        package.filenames.each do |filename|
          src = File.join(Config.tmp_path, filename)
          dest = File.join(remote_path, filename)
          Logger.info "Storing '#{ dest }'..."

          ::Qiniu.upload_file(uptoken: ::Qiniu.generate_upload_token,
                              bucket: bucket,
                              file: src,
                              key: dest)
        end
      end

      # Called by the Cycler.
      # Any error raised will be logged as a warning.
      def remove!(package)
        Logger.info "Removing backup package dated #{ package.time }..."
        remote_path = remote_path_for(package)
        package.filenames.each do |filename|
          ::Qiniu.delete(bucket, File.join(remote_path, filename))
        end
      end

      def check_configuration
         required = %w{ access_key secret_key bucket }

        raise Error, <<-EOS if required.map {|name| send(name) }.any?(&:nil?)
          Configuration Error
          #{ required.map {|name| "##{ name }"}.join(', ') } are all required
        EOS
      end

      def config_credentials
        ::Qiniu.establish_connection!(access_key: access_key, secret_key: secret_key)
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 4 rubygems

Version Path
backup-ssh-4.4.5 lib/backup/storage/qiniu.rb
backup-remote-0.0.19 lib/backup/storage/qiniu.rb
cm-backup-1.0.0 lib/backup/storage/qiniu.rb
backup-ssh-4.4.4 lib/backup/storage/qiniu.rb
backup-ssh-4.4.3 lib/backup/storage/qiniu.rb
backup-4.4.1 lib/backup/storage/qiniu.rb
backup-remote-0.0.18 lib/backup/storage/qiniu.rb
backup-remote-0.0.17 lib/backup/storage/qiniu.rb
backup-remote-0.0.16 lib/backup/storage/qiniu.rb
backup-remote-0.0.15 lib/backup/storage/qiniu.rb
backup-ssh-4.4.2 lib/backup/storage/qiniu.rb
backup-ssh-4.4.1 lib/backup/storage/qiniu.rb
backup-remote-0.0.14 lib/backup/storage/qiniu.rb
backup-remote-0.0.13 lib/backup/storage/qiniu.rb
backup-remote-0.0.12 lib/backup/storage/qiniu.rb
backup-remote-0.0.11 lib/backup/storage/qiniu.rb
backup-ssh-4.4.0 lib/backup/storage/qiniu.rb
backup-remote-0.0.10 lib/backup/storage/qiniu.rb
backup-remote-0.0.9 lib/backup/storage/qiniu.rb
backup-remote-0.0.8 lib/backup/storage/qiniu.rb