Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acl & success_action_status options + PR18 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/refile/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ class S3
# @param [Hash] s3_options Additional options to initialize S3 with
# @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Configuration.html
# @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3.html
def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options)
def initialize(region:, bucket:, max_size: nil, prefix: nil, acl: "public-read", hasher: Refile::RandomHasher.new, **s3_options)
@s3_options = { region: region }.merge s3_options
@s3 = Aws::S3::Resource.new @s3_options
credentials = @s3.client.config.credentials
credentials = @s3.client.config.credentials.credentials
raise S3CredentialsError unless credentials
@access_key_id = credentials.access_key_id
@bucket_name = bucket
@bucket = @s3.bucket @bucket_name
@hasher = hasher
@prefix = prefix
@max_size = max_size
@acl = acl
end

# Upload a file into this backend
Expand All @@ -61,9 +62,9 @@ def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::Ran
id = @hasher.hash(uploadable)

if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(S3) and uploadable.backend.access_key_id == access_key_id
object(id).copy_from(copy_source: [@bucket_name, uploadable.backend.object(uploadable.id).key].join("/"))
object(id).copy_from(copy_source: [@bucket_name, uploadable.backend.object(uploadable.id).key].join("/"), acl: @acl)
else
object(id).put(body: uploadable, content_length: uploadable.size)
object(id).put(body: uploadable, content_length: uploadable.size, acl: @acl)
end

Refile::File.new(self, id)
Expand Down Expand Up @@ -143,9 +144,10 @@ def clear!(confirm = nil)
# backend directly.
#
# @return [Refile::Signature]
#http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Bucket.html#presigned_post-instance_method
def presign
id = RandomHasher.new.hash
signature = @bucket.presigned_post(key: [*@prefix, id].join("/"))
signature = @bucket.presigned_post(key: [*@prefix, id].join("/"), success_action_status: "201", acl: @acl)
signature.content_length_range(0..@max_size) if @max_size
Signature.new(as: "file", id: id, url: signature.url.to_s, fields: signature.fields)
end
Expand All @@ -154,4 +156,4 @@ def presign
@bucket.object([*@prefix, id].join("/"))
end
end
end
end