Skip to content

Commit

Permalink
Refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmorrison committed May 22, 2024
1 parent 2f809fd commit 8188e92
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
23 changes: 21 additions & 2 deletions lib/io_streams/paths/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class S3 < IOStreams::Path
# Largest file size supported by the S3 copy object api.
S3_COPY_OBJECT_SIZE_LIMIT = 5 * 1024 * 1024 * 1024

# When an upload file exceeds this size, use a multipart file upload.
MULTIPART_UPLOAD_SIZE = 5 * 1024 * 1024

# Arguments:
#
# url: [String]
Expand All @@ -24,6 +27,21 @@ class S3 < IOStreams::Path
# secret_access_key: [String]
# AWS Secret Access Key Id to use to access this bucket.
#
# region: [String]
# The AWS region to connect to.
# Defaults to region set in environment variable, or credential files.
#
# client: [Aws::S3::Client | Hash]
# Supply the AWS S3 Client instance to use for this path.
# Or, when a Hash, build a new client using the hash parameters.
#
# Example:
# client = Aws::S3::Client.new(endpoint: "https://s3.test.com")
# IOStreams::Paths::S3.new(client: client)
#
# Example:
# IOStreams::Paths::S3.new(client: { endpoint: "https://s3.test.com" })
#
# Writer specific options:
#
# @option params [String] :acl
Expand Down Expand Up @@ -133,7 +151,7 @@ class S3 < IOStreams::Path
#
# @option params [String] :object_lock_legal_hold_status
# The Legal Hold status that you want to apply to the specified object.
def initialize(url, client: nil, access_key_id: nil, secret_access_key: nil, **args)
def initialize(url, client: nil, access_key_id: nil, secret_access_key: nil, region: nil, **args)
Utils.load_soft_dependency("aws-sdk-s3", "AWS S3") unless defined?(::Aws::S3::Client)

uri = Utils::URI.new(url)
Expand All @@ -148,6 +166,7 @@ def initialize(url, client: nil, access_key_id: nil, secret_access_key: nil, **a
@client_options = client.is_a?(Hash) ? client.dup : {}
@client_options[:access_key_id] = access_key_id if access_key_id
@client_options[:secret_access_key] = secret_access_key if secret_access_key
@client_options[:region] = region if region
end

@options = args
Expand Down Expand Up @@ -269,7 +288,7 @@ def stream_writer(&block)

# Shortcut method if caller has a filename already with no other streams applied:
def write_file(file_name)
if ::File.size(file_name) > 5 * 1024 * 1024
if ::File.size(file_name) > MULTIPART_UPLOAD_SIZE
# Use multipart file upload
s3 = Aws::S3::Resource.new(client: client)
obj = s3.bucket(bucket_name).object(path)
Expand Down
11 changes: 4 additions & 7 deletions lib/io_streams/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ def self.load_soft_dependency(gem_name, stream_type, require_name = gem_name)

# Helper method: Returns [true|false] if a value is blank?
def self.blank?(value)
if value.nil?
true
elsif value.is_a?(String)
value !~ /\S/
else
value.respond_to?(:empty?) ? value.empty? : !value
end
return true if value.nil?
return value !~ /\S/ if value.is_a?(String)

value.respond_to?(:empty?) ? value.empty? : !value
end

# Yields the path to a temporary file_name.
Expand Down

0 comments on commit 8188e92

Please sign in to comment.