Sha256: e86bc0e2b70704b2d5066a836012f8b0ef56fcac7f4cd7a3b5b49a3966a652f6

Contents?: true

Size: 996 Bytes

Versions: 4

Compression:

Stored size: 996 Bytes

Contents

require 's3'
require 'retry-this'

class S3Service
	def initialize(key_id, key_secret, bucket, options = {})
		@options = options
		@logger = (options[:logger] or Logger.new('/dev/null'))

		@s3 = S3::Service.new(:access_key_id => key_id, :secret_access_key => key_secret)

		@logger.info "Getting bucket: #{bucket}"
		@bucket = @s3.buckets.find(bucket) or fail "no buckte '#{bucket}' found"
	end

	def put_image(image_path, content_type, data)
		@logger.info "Putting image in bucket '#{@bucket.name}': #{image_path}"

		file = @bucket.objects.build(image_path)
		file.content_type = content_type
		file.content = data

		RetryThis.retry_this(
			:times => (@options[:upload_retry_times] or 1),
			:sleep => (@options[:upload_retry_delay] or 0.0),
			:error_types => [Errno::ECONNRESET, Timeout::Error, S3::Error::RequestTimeout]
		) do |attempt|
			@logger.warn "Retrying S3 save operation" if attempt > 1
			file.save
		end

		"http://#{@bucket.name}.s3.amazonaws.com/#{image_path}"
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
httpimagestore-0.3.2 lib/httpimagestore/s3_service.rb
httpimagestore-0.3.1 lib/httpimagestore/s3_service.rb
httpimagestore-0.3.0 lib/httpimagestore/s3_service.rb
httpimagestore-0.2.4 lib/httpimagestore/s3_service.rb