Skip to content

Commit 57e6417

Browse files
committed
Merge pull request #2 from artempartos/develop
make aws keys optional (for use IAM S3 role)
2 parents 4208dc3 + 07c7d6c commit 57e6417

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/refile/s3.rb

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
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
1021
# backend = Refile::Backend::S3.new(
11-
# access_key_id: "xyz",
12-
# secret_access_key: "abcd1234",
1322
# region: "sa-east-1",
1423
# bucket: "my-bucket",
1524
# prefix: "files"
@@ -21,10 +30,8 @@ class S3
2130

2231
attr_reader :access_key_id, :max_size
2332

24-
# Sets up an S3 backend with the given credentials.
33+
# Sets up an S3 backend
2534
#
26-
# @param [String] access_key_id
27-
# @param [String] secret_access_key
2835
# @param [String] region The AWS region to connect to
2936
# @param [String] bucket The name of the bucket where files will be stored
3037
# @param [String] prefix A prefix to add to all files. Prefixes on S3 are kind of like folders.
@@ -33,11 +40,12 @@ class S3
3340
# @param [Hash] s3_options Additional options to initialize S3 with
3441
# @see http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Configuration.html
3542
# @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
4045
@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
4149
@bucket_name = bucket
4250
@bucket = @s3.bucket @bucket_name
4351
@hasher = hasher

0 commit comments

Comments
 (0)