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
10
21
# backend = Refile::Backend::S3.new(
11
- # access_key_id: "xyz",
12
- # secret_access_key: "abcd1234",
13
22
# region: "sa-east-1",
14
23
# bucket: "my-bucket",
15
24
# prefix: "files"
@@ -21,10 +30,8 @@ class S3
21
30
22
31
attr_reader :access_key_id , :max_size
23
32
24
- # Sets up an S3 backend with the given credentials.
33
+ # Sets up an S3 backend
25
34
#
26
- # @param [String] access_key_id
27
- # @param [String] secret_access_key
28
35
# @param [String] region The AWS region to connect to
29
36
# @param [String] bucket The name of the bucket where files will be stored
30
37
# @param [String] prefix A prefix to add to all files. Prefixes on S3 are kind of like folders.
@@ -33,11 +40,12 @@ class S3
33
40
# @param [Hash] s3_options Additional options to initialize S3 with
34
41
# @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Configuration.html
35
42
# @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3.html
36
- def initialize ( access_key_id :, secret_access_key :, region :, bucket :, max_size : nil , prefix : nil , hasher : Refile ::RandomHasher . new , **s3_options )
37
- @access_key_id = access_key_id
38
- @secret_access_key = secret_access_key
39
- @s3_options = { access_key_id : access_key_id , secret_access_key : secret_access_key , region : region } . merge s3_options
43
+ def initialize ( region :, bucket :, max_size : nil , prefix : nil , hasher : Refile ::RandomHasher . new , **s3_options )
44
+ @s3_options = { region : region } . merge s3_options
40
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
41
49
@bucket_name = bucket
42
50
@bucket = @s3 . bucket @bucket_name
43
51
@hasher = hasher
0 commit comments