Sha256: 4e64864c8f77f6fe98f985dc23a4f5d38ea9761d97d1496046f601ed66280beb

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

module Astrails
  module Safe
    class S3 < Sink

      protected

      def active?
        bucket && key && secret
      end

      def path
        @path ||= expand(config[:s3, :path] || config[:local, :path] || ":kind/:id")
      end

      def save
        raise RuntimeError, "pipe-streaming not supported for S3." unless @backup.path

        # needed in cleanup even on dry run
        AWS::S3::Base.establish_connection!(:access_key_id => key, :secret_access_key => secret, :use_ssl => true) unless $LOCAL

        puts "Uploading #{bucket}:#{full_path}" if $_VERBOSE || $DRY_RUN
        unless $DRY_RUN || $LOCAL
          benchmark = Benchmark.realtime do
            AWS::S3::Bucket.create(bucket)
            File.open(@backup.path) do |file|
              AWS::S3::S3Object.store(full_path, file, bucket)
            end
          end
          puts "...done" if $_VERBOSE
          puts("Upload took " + sprintf("%.2f", benchmark) + " second(s).") if $_VERBOSE
        end
      end

      def cleanup
        return if $LOCAL

        return unless keep = @config[:keep, :s3]


        puts "listing files: #{bucket}:#{base}*" if $_VERBOSE
        files = AWS::S3::Bucket.objects(bucket, :prefix => base, :max_keys => keep * 2)
        puts files.collect {|x| x.key} if $_VERBOSE

        files = files.
          collect {|x| x.key}.
          sort

        cleanup_with_limit(files, keep) do |f|
          puts "removing s3 file #{bucket}:#{f}" if $DRY_RUN || $_VERBOSE
          AWS::S3::Bucket.find(bucket)[f].delete unless $DRY_RUN || $LOCAL
        end
      end

      def bucket
        @config[:s3, :bucket]
      end

      def key
        @config[:s3, :key]
      end

      def secret
        @config[:s3, :secret]
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
astrails-safe-0.2.2 lib/astrails/safe/s3.rb
astrails-safe-0.2.3 lib/astrails/safe/s3.rb
bostonlogic-safe-0.3.0 lib/astrails/safe/s3.rb
astrails-safe-0.2.5 lib/astrails/safe/s3.rb
webbynode-safe-0.2.5 lib/astrails/safe/s3.rb
astrails-safe-0.2.4 lib/astrails/safe/s3.rb