Skip to content

Commit

Permalink
acl & success_action_status options + PR18
Browse files Browse the repository at this point in the history
1)add acl option to initialise method and use in available calls
defaults to ‘public-read’ so backwards compatible, but enables people
to make private as desired.
2) also included deprecation fix from PR refile#18
3) added success_action_status: 201 to presign method to fix firefox
xml parse error:
XML Parsing Error: no element found Location:
moz-nullprincipal:{28d28eb9-bcef-f842-8355-54e833de9cd1} Line Number 1,
Column 1:
  • Loading branch information
fran-worley committed Dec 23, 2015
1 parent 78a74b6 commit 7dcc1b7
Showing 1 changed file with 8 additions and 6 deletions.
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

0 comments on commit 7dcc1b7

Please sign in to comment.