lib/io_streams/paths/s3.rb in iostreams-1.3.2 vs lib/io_streams/paths/s3.rb in iostreams-1.3.3
- old
+ new
@@ -1,11 +1,11 @@
require "uri"
module IOStreams
module Paths
class S3 < IOStreams::Path
- attr_reader :bucket_name, :client
+ attr_reader :bucket_name, :client, :options
# Arguments:
#
# url: [String]
# Prefix must be: `s3://`
@@ -177,21 +177,40 @@
# Moves this file to the `target_path` by copying it to the new name and then deleting the current file.
#
# Notes:
# - Can copy across buckets.
+ # - No stream conversions are applied.
def move_to(target_path)
+ target = copy_to(target_path, convert: false)
+ delete
+ target
+ end
+
+ # Make S3 perform direct copies within S3 itself.
+ def copy_to(target_path, convert: true)
+ return super(target_path) if convert
+
target = IOStreams.new(target_path)
return super(target) unless target.is_a?(self.class)
source_name = ::File.join(bucket_name, path)
- # TODO: Does/should it also copy metadata?
- client.copy_object(bucket: target.bucket_name, key: target.path, copy_source: source_name)
- delete
+ client.copy_object(options.merge(bucket: target.bucket_name, key: target.path, copy_source: source_name))
target
end
+ # Make S3 perform direct copies within S3 itself.
+ def copy_from(source_path, convert: true)
+ return super(source_path) if convert
+
+ source = IOStreams.new(source_path)
+ return super(source, **args) unless source.is_a?(self.class)
+
+ source_name = ::File.join(source.bucket_name, source.path)
+ client.copy_object(options.merge(bucket: bucket_name, key: path, copy_source: source_name))
+ end
+
# S3 logically creates paths when a key is set.
def mkpath
self
end
@@ -218,11 +237,11 @@
end
# Shortcut method if caller has a filename already with no other streams applied:
def read_file(file_name)
::File.open(file_name, "wb") do |file|
- client.get_object(@options.merge(response_target: file, bucket: bucket_name, key: path))
+ client.get_object(options.merge(response_target: file, bucket: bucket_name, key: path))
end
end
# Write to AWS S3
#
@@ -246,13 +265,13 @@
def write_file(file_name)
if ::File.size(file_name) > 5 * 1024 * 1024
# Use multipart file upload
s3 = Aws::S3::Resource.new(client: client)
obj = s3.bucket(bucket_name).object(path)
- obj.upload_file(file_name)
+ obj.upload_file(file_name, options)
else
::File.open(file_name, "rb") do |file|
- client.put_object(@options.merge(bucket: bucket_name, key: path, body: file))
+ client.put_object(options.merge(bucket: bucket_name, key: path, body: file))
end
end
end
# Notes: