Skip to content

Commit 07c7d6c

Browse files
committed
add errors and correct access_key_id
1 parent d75c94e commit 07c7d6c

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/refile/s3.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
require "refile/s3/version"
55

66
module Refile
7+
8+
# @api private
9+
class S3BackendError < StandardError; end
10+
11+
# @api private
12+
class S3CredentialsError < S3BackendError
13+
def message
14+
"Credentials not found"
15+
end
16+
end
17+
718
# A refile backend which stores files in Amazon S3
819
#
920
# @example
@@ -17,9 +28,9 @@ module Refile
1728
class S3
1829
extend Refile::BackendMacros
1930

20-
attr_reader :max_size
31+
attr_reader :access_key_id, :max_size
2132

22-
# Sets up an S3 backend with the given credentials.
33+
# Sets up an S3 backend
2334
#
2435
# @param [String] region The AWS region to connect to
2536
# @param [String] bucket The name of the bucket where files will be stored
@@ -32,6 +43,9 @@ class S3
3243
def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options)
3344
@s3_options = { region: region }.merge s3_options
3445
@s3 = Aws::S3::Resource.new @s3_options
46+
credentials = @s3.client.config.credentials
47+
raise S3CredentialsError unless credentials
48+
@access_key_id = credentials.access_key_id
3549
@bucket_name = bucket
3650
@bucket = @s3.bucket @bucket_name
3751
@hasher = hasher
@@ -46,7 +60,7 @@ def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::Ran
4660
verify_uploadable def upload(uploadable)
4761
id = @hasher.hash(uploadable)
4862

49-
if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(S3)
63+
if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(S3) and uploadable.backend.access_key_id == access_key_id
5064
object(id).copy_from(copy_source: [@bucket_name, uploadable.backend.object(uploadable.id).key].join("/"))
5165
else
5266
object(id).put(body: uploadable, content_length: uploadable.size)

0 commit comments

Comments
 (0)