Sha256: 59af165650ad07bf9765d3cd3cbf0503d7a231c88163191af4688a5f5fdcb8d6

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

module Astrails
  module Safe
    class S3 < Sink

      protected

      def active?
        bucket && key && secret
      end

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

      def save
        # 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

        file = @parent.open
        puts "Uploading #{bucket}:#{path}" if $_VERBOSE || $DRY_RUN
        unless $DRY_RUN || $LOCAL
          AWS::S3::Bucket.create(bucket)
          AWS::S3::S3Object.store(path, file, bucket)
          puts "...done" if $_VERBOSE
        end
        file.close if file

      end

      def cleanup

        return if $LOCAL

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

        bucket = @config[:s3, :bucket]

        base = File.basename(filename).split(".").first

        puts "listing files in #{bucket}:#{prefix}/#{base}"
        files = AWS::S3::Bucket.objects(bucket, :prefix => "#{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

8 entries across 8 versions & 4 rubygems

Version Path
astrails-safe-0.1.1 lib/astrails/safe/s3.rb
astrails-safe-0.1.2 lib/astrails/safe/s3.rb
astrails-safe-0.1.3 lib/astrails/safe/s3.rb
astrails-safe-0.1.4 lib/astrails/safe/s3.rb
astrails-safe-0.1.6 lib/astrails/safe/s3.rb
colin-safe-0.1.6 lib/astrails/safe/s3.rb
markmansour-safe-0.1.7 lib/astrails/safe/s3.rb
ralph-safe-0.1.7 lib/astrails/safe/s3.rb