4
4
require "refile/s3/version"
5
5
6
6
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
+
7
18
# A refile backend which stores files in Amazon S3
8
19
#
9
20
# @example
@@ -17,9 +28,9 @@ module Refile
17
28
class S3
18
29
extend Refile ::BackendMacros
19
30
20
- attr_reader :max_size
31
+ attr_reader :access_key_id , : max_size
21
32
22
- # Sets up an S3 backend with the given credentials.
33
+ # Sets up an S3 backend
23
34
#
24
35
# @param [String] region The AWS region to connect to
25
36
# @param [String] bucket The name of the bucket where files will be stored
@@ -32,6 +43,9 @@ class S3
32
43
def initialize ( region :, bucket :, max_size : nil , prefix : nil , hasher : Refile ::RandomHasher . new , **s3_options )
33
44
@s3_options = { region : region } . merge s3_options
34
45
@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
35
49
@bucket_name = bucket
36
50
@bucket = @s3 . bucket @bucket_name
37
51
@hasher = hasher
@@ -46,7 +60,7 @@ def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::Ran
46
60
verify_uploadable def upload ( uploadable )
47
61
id = @hasher . hash ( uploadable )
48
62
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
50
64
object ( id ) . copy_from ( copy_source : [ @bucket_name , uploadable . backend . object ( uploadable . id ) . key ] . join ( "/" ) )
51
65
else
52
66
object ( id ) . put ( body : uploadable , content_length : uploadable . size )
0 commit comments