lib/tori/backend/s3.rb in tori-0.0.4 vs lib/tori/backend/s3.rb in tori-0.0.5
- old
+ new
@@ -1,47 +1,90 @@
-require 'aws-sdk'
+require 'aws-sdk-core'
module Tori
module Backend
class S3
- # You must be set bucket name.
- # And you can configurate to S3
- # But, you can also configurate by AWS.config()
+ attr_accessor :bucket, :client
+ # Must be set bucket name.
+ # And it use aws-sdk-core >= 2.0
+ # ENV["TORI_ACCESS_KEY"] > aws-sdk credentials
#
# example:
- # Tori.config.backend = Tori::Backend::S3.new(
- # bucket: 'photos',
- # region: '...',
- # s3_encryption_key: '...'
- # )
- def initialize(bucket:, **s3_config)
- s3 = AWS::S3.new(s3_config)
- @bucket = s3.buckets[bucket]
+ # Tori.config.backend = Tori::Backend::S3.new(bucket: 'tori_bucket')
+ def initialize(bucket:)
+ @bucket = bucket
+ @client = if ENV["TORI_AWS_ACCESS_KEY_ID"] && ENV["TORI_AWS_SECRET_ACCESS_KEY"]
+ Aws::S3::Client.new(
+ access_key_id: ENV["TORI_AWS_ACCESS_KEY_ID"],
+ secret_access_key: ENV["TORI_AWS_SECRET_ACCESS_KEY"],
+ region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region],
+ )
+ else
+ Aws::S3::Client.new(
+ region: ENV["TORI_AWS_REGION"] || ENV['AWS_REGION'] || Aws.config[:region]
+ )
+ end
end
- def copy(form_path, filename)
- object(filename).write(file: form_path)
+ def write(filename, resource)
+ case resource
+ when IO
+ put filename, f
+ when String
+ put filename, resource
+ else
+ ::File.open(resource.to_path) { |f| put filename, f }
+ end
end
def delete(filename)
- object(filename).delete
+ @client.delete_object(
+ bucket: @bucket,
+ key: filename
+ )
end
- def exist?(filename)
- object(filename).exists?
+ def exist?(filename = nil)
+ head filename
+ rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NotFound
+ false
+ else
+ true
end
alias exists? exist?
def read(filename)
- object(filename).read
+ @client.get_object(
+ bucket: @bucket,
+ key: filename
+ )[:body].read
end
- def url(filename)
- object(filename).url_for(:read)
+ def public_url(filename, params={})
+ "#{@client.config.endpoint}/#{@bucket}/#{filename}"
end
- def object(filename)
- @bucket.objects[filename]
+ def url_for(filename, method)
+ signer = Aws::S3::Presigner.new(client: @client)
+ signer.presigned_url(method, bucket: @bucket, key: filename)
+ end
+
+ private
+
+ def put(filename, body)
+ @client.put_object(
+ bucket: @bucket,
+ key: filename,
+ body: body
+ )
+ end
+
+ def head(filename = nil)
+ if filename
+ @client.head_object bucket: @bucket, key: filename
+ else
+ @client.head_bucket bucket: @bucket
+ end
end
end
end
end