Skip to content

make aws keys optional (for use IAM S3 role) #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/refile/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
require "refile/s3/version"

module Refile

# @api private
class S3BackendError < StandardError; end

# @api private
class S3CredentialsError < S3BackendError
def message
"Credentials not found"
end
end

# A refile backend which stores files in Amazon S3
#
# @example
# backend = Refile::Backend::S3.new(
# access_key_id: "xyz",
# secret_access_key: "abcd1234",
# region: "sa-east-1",
# bucket: "my-bucket",
# prefix: "files"
Expand All @@ -21,10 +30,8 @@ class S3

attr_reader :access_key_id, :max_size

# Sets up an S3 backend with the given credentials.
# Sets up an S3 backend
#
# @param [String] access_key_id
# @param [String] secret_access_key
# @param [String] region The AWS region to connect to
# @param [String] bucket The name of the bucket where files will be stored
# @param [String] prefix A prefix to add to all files. Prefixes on S3 are kind of like folders.
Expand All @@ -33,11 +40,12 @@ 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(access_key_id:, secret_access_key:, region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options)
@access_key_id = access_key_id
@secret_access_key = secret_access_key
@s3_options = { access_key_id: access_key_id, secret_access_key: secret_access_key, region: region }.merge s3_options
def initialize(region:, bucket:, max_size: nil, prefix: nil, 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
raise S3CredentialsError unless credentials
@access_key_id = credentials.access_key_id
@bucket_name = bucket
@bucket = @s3.bucket @bucket_name
@hasher = hasher
Expand Down