iostreams 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -22
- data/Rakefile +45 -0
- data/docs/CLAUDE.md +9 -0
- data/docs/config.md +161 -0
- data/docs/copy_files.md +93 -0
- data/docs/extensions.md +125 -0
- data/docs/formats.md +816 -0
- data/docs/index.md +388 -0
- data/docs/path.md +668 -0
- data/docs/pgp.md +436 -0
- data/docs/streams.md +382 -0
- data/docs/tutorial.md +495 -0
- data/docs/upgrading.md +664 -0
- data/lib/io_streams/builder/deprecations.rb +250 -0
- data/lib/io_streams/builder.rb +316 -39
- data/lib/io_streams/bzip2/reader.rb +41 -2
- data/lib/io_streams/bzip2/writer.rb +35 -2
- data/lib/io_streams/deprecation/methods.rb +51 -0
- data/lib/io_streams/deprecation/once.rb +27 -0
- data/lib/io_streams/deprecation/option_names.rb +26 -0
- data/lib/io_streams/deprecation/words.rb +42 -0
- data/lib/io_streams/deprecation.rb +259 -0
- data/lib/io_streams/encode/reader.rb +58 -3
- data/lib/io_streams/encode/writer.rb +23 -2
- data/lib/io_streams/encode.rb +99 -0
- data/lib/io_streams/errors.rb +55 -0
- data/lib/io_streams/gzip/reader.rb +15 -0
- data/lib/io_streams/gzip/writer.rb +11 -2
- data/lib/io_streams/io_streams.rb +468 -28
- data/lib/io_streams/line/reader.rb +9 -3
- data/lib/io_streams/line/utf8_reader.rb +23 -0
- data/lib/io_streams/path.rb +227 -28
- data/lib/io_streams/paths/file.rb +115 -6
- data/lib/io_streams/paths/http.rb +92 -5
- data/lib/io_streams/paths/matcher.rb +6 -3
- data/lib/io_streams/paths/s3.rb +242 -20
- data/lib/io_streams/paths/sftp/net_ssh.rb +104 -0
- data/lib/io_streams/paths/sftp.rb +141 -57
- data/lib/io_streams/paths.rb +23 -0
- data/lib/io_streams/pgp/reader.rb +60 -2
- data/lib/io_streams/pgp/writer.rb +42 -6
- data/lib/io_streams/pgp.rb +92 -21
- data/lib/io_streams/reader.rb +54 -1
- data/lib/io_streams/record/reader.rb +71 -10
- data/lib/io_streams/record/writer.rb +17 -10
- data/lib/io_streams/row/header_row.rb +21 -0
- data/lib/io_streams/row/reader.rb +52 -15
- data/lib/io_streams/row/writer.rb +64 -13
- data/lib/io_streams/schema/columns.rb +456 -0
- data/lib/io_streams/schema/constraints.rb +99 -0
- data/lib/io_streams/schema/field.rb +967 -0
- data/lib/io_streams/schema/json_number.rb +56 -0
- data/lib/io_streams/schema/keys.rb +78 -0
- data/lib/io_streams/schema/pattern.rb +257 -0
- data/lib/io_streams/schema/source.rb +31 -0
- data/lib/io_streams/schema.rb +616 -0
- data/lib/io_streams/stream.rb +843 -69
- data/lib/io_streams/symmetric_encryption/reader.rb +12 -0
- data/lib/io_streams/symmetric_encryption/writer.rb +4 -0
- data/lib/io_streams/tabular/header.rb +60 -4
- data/lib/io_streams/tabular/header_mapping.rb +332 -0
- data/lib/io_streams/tabular/mapping.rb +165 -0
- data/lib/io_streams/tabular/parser/array.rb +5 -0
- data/lib/io_streams/tabular/parser/base.rb +34 -0
- data/lib/io_streams/tabular/parser/csv.rb +5 -0
- data/lib/io_streams/tabular/parser/fixed.rb +200 -1
- data/lib/io_streams/tabular/parser/fixed_fields.rb +160 -0
- data/lib/io_streams/tabular/parser/hash.rb +10 -0
- data/lib/io_streams/tabular/parser/json.rb +17 -0
- data/lib/io_streams/tabular/parser/json_fields.rb +132 -0
- data/lib/io_streams/tabular/parser/psv.rb +6 -2
- data/lib/io_streams/tabular/schema_mapping.rb +361 -0
- data/lib/io_streams/tabular.rb +344 -30
- data/lib/io_streams/utils.rb +467 -1
- data/lib/io_streams/version.rb +1 -1
- data/lib/io_streams/writer.rb +37 -1
- data/lib/io_streams/xlsx/cells.rb +127 -0
- data/lib/io_streams/xlsx/reader.rb +61 -1
- data/lib/io_streams/xlsx/schema_reader.rb +90 -0
- data/lib/io_streams/xlsx/v3_writer.rb +16 -0
- data/lib/io_streams/zip/reader.rb +11 -0
- data/lib/io_streams/zip/writer.rb +28 -2
- data/lib/iostreams.rb +21 -17
- metadata +50 -7
|
@@ -6,6 +6,19 @@ module IOStreams
|
|
|
6
6
|
class HTTP < IOStreams::Path
|
|
7
7
|
attr_reader :username, :password, :http_redirect_count, :url
|
|
8
8
|
|
|
9
|
+
# The password, and the query `parameters`, which can hold an access key, see IOStreams::Path.redact_options.
|
|
10
|
+
def self.sensitive_option_names = %i[password parameters]
|
|
11
|
+
|
|
12
|
+
# Returns [String|nil] the deprecation warning for an HTTP(S) url without a path, such as `https://host`, or
|
|
13
|
+
# `https://host?a=1`, which IOStreams v3.0 reads with the path `/`, as `https://host/?a=1`, see
|
|
14
|
+
# `IOStreams::Path.name_deprecation`.
|
|
15
|
+
def self.name_deprecation(url)
|
|
16
|
+
return unless url_without_path?(url)
|
|
17
|
+
|
|
18
|
+
"an HTTP url without a path, such as https://host or https://host?a=1, is deprecated, since v3.0 adds a / " \
|
|
19
|
+
"after the host, https://host/?a=1, which changes its #to_s and #path: supply the url with the /"
|
|
20
|
+
end
|
|
21
|
+
|
|
9
22
|
# Stream to/from a remote file over http(s).
|
|
10
23
|
#
|
|
11
24
|
# Parameters:
|
|
@@ -18,6 +31,12 @@ module IOStreams
|
|
|
18
31
|
# Full url showing all the optional elements that can be set via the url:
|
|
19
32
|
# https://username:password@hostname/path/file_name
|
|
20
33
|
#
|
|
34
|
+
# SECURITY WARNING:
|
|
35
|
+
# A username and password supplied in the url remain part of it, so `#to_s` and `#url`
|
|
36
|
+
# return them, as does any log or error message that includes the path.
|
|
37
|
+
# Supply them with the `username:` and `password:` arguments instead,
|
|
38
|
+
# and log `#display_name`, which never includes them.
|
|
39
|
+
#
|
|
21
40
|
# username: [String]
|
|
22
41
|
# When supplied, basic authentication is used with the username and password.
|
|
23
42
|
#
|
|
@@ -67,6 +86,7 @@ module IOStreams
|
|
|
67
86
|
@maximum_file_size = maximum_file_size
|
|
68
87
|
@url = parameters ? "#{url}?#{URI.encode_www_form(parameters)}" : url
|
|
69
88
|
super(uri.path)
|
|
89
|
+
warn_url_without_host(uri)
|
|
70
90
|
end
|
|
71
91
|
|
|
72
92
|
# Does not support relative file names since there is no concept of current working directory
|
|
@@ -78,17 +98,48 @@ module IOStreams
|
|
|
78
98
|
url
|
|
79
99
|
end
|
|
80
100
|
|
|
101
|
+
# Returns [String] the url without the user name, password, query or fragment, which can hold credentials,
|
|
102
|
+
# such as the signature of a pre-signed url, or the `parameters:` supplied to this path.
|
|
103
|
+
def display_name
|
|
104
|
+
uri = URI.parse(url)
|
|
105
|
+
uri.user = nil
|
|
106
|
+
uri.query = nil
|
|
107
|
+
uri.fragment = nil
|
|
108
|
+
uri.to_s
|
|
109
|
+
end
|
|
110
|
+
|
|
81
111
|
private
|
|
82
112
|
|
|
83
113
|
attr_reader :allow_hosts, :maximum_file_size
|
|
84
114
|
|
|
115
|
+
# Reports a deprecation warning for a url without a host, such as `http:///a.csv`, which IOStreams 3.0 raises
|
|
116
|
+
# ArgumentError for when the path is created, so that a mistake in a configured url is reported wherever the
|
|
117
|
+
# code runs.
|
|
118
|
+
def warn_url_without_host(uri)
|
|
119
|
+
return unless uri.host.to_s.empty?
|
|
120
|
+
|
|
121
|
+
Deprecation.warn("the HTTP url #{display_name.inspect} is deprecated, since it names no host, name one, such " \
|
|
122
|
+
"as 'https://example.org/a.csv'")
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The methods of the IO that v3.0 reads the response from, see #v3_source: those of an IO that read, and #close.
|
|
126
|
+
V3_SOURCE_METHODS = [*IOStreams::Reader::V3_READ_METHODS, :close].freeze
|
|
127
|
+
private_constant :V3_SOURCE_METHODS
|
|
128
|
+
|
|
129
|
+
# Returns [Array(Array<Symbol>, String)] the methods of the IO that v3.0 reads the response from, in place of the
|
|
130
|
+
# temp File that v2.2 downloads it into, and its name, see `IOStreams::Stream#v3_source`: v3.0 reads the response as
|
|
131
|
+
# the application reads it, with an IO that only reads.
|
|
132
|
+
def v3_source
|
|
133
|
+
[V3_SOURCE_METHODS, "the IO of an HTTP path"]
|
|
134
|
+
end
|
|
135
|
+
|
|
85
136
|
# Read a file using an http get.
|
|
86
137
|
#
|
|
87
138
|
# For example:
|
|
88
139
|
# IOStreams.path('https://www5.fdic.gov/idasp/Offices2.zip').reader {|file| puts file.read}
|
|
89
140
|
#
|
|
90
141
|
# Read the file without unzipping and streaming the first file in the zip:
|
|
91
|
-
# IOStreams.path('https://www5.fdic.gov/idasp/Offices2.zip').
|
|
142
|
+
# IOStreams.path('https://www5.fdic.gov/idasp/Offices2.zip').raw.reader {|file| puts file.read}
|
|
92
143
|
#
|
|
93
144
|
# Notes:
|
|
94
145
|
# * Since Net::HTTP download only supports a push stream, the data is streamed into a tempfile first.
|
|
@@ -108,7 +159,9 @@ module IOStreams
|
|
|
108
159
|
request.basic_auth(username, password) if username && same_origin?(uri)
|
|
109
160
|
|
|
110
161
|
http.request(request) do |response|
|
|
111
|
-
|
|
162
|
+
if response.is_a?(Net::HTTPNotFound)
|
|
163
|
+
raise(IOStreams::Errors::CommunicationsFailure, "Invalid URL: #{without_credentials(uri)}")
|
|
164
|
+
end
|
|
112
165
|
if response.is_a?(Net::HTTPUnauthorized)
|
|
113
166
|
raise(IOStreams::Errors::CommunicationsFailure, "Authorization Required: Invalid :username or :password.")
|
|
114
167
|
end
|
|
@@ -117,7 +170,10 @@ module IOStreams
|
|
|
117
170
|
raise(IOStreams::Errors::CommunicationsFailure, "Too many redirects") if http_redirect_count < 1
|
|
118
171
|
|
|
119
172
|
location = response["location"]
|
|
120
|
-
|
|
173
|
+
unless location
|
|
174
|
+
raise(IOStreams::Errors::CommunicationsFailure,
|
|
175
|
+
"Redirect missing location header: #{without_credentials(uri)}")
|
|
176
|
+
end
|
|
121
177
|
|
|
122
178
|
# Resolve relative redirects against the current uri.
|
|
123
179
|
new_uri = uri.merge(location)
|
|
@@ -129,7 +185,7 @@ module IOStreams
|
|
|
129
185
|
end
|
|
130
186
|
|
|
131
187
|
# Since Net::HTTP download only supports a push stream, write it to a tempfile first.
|
|
132
|
-
Utils.
|
|
188
|
+
Utils.private_temp_file("iostreams_http") do |file_name|
|
|
133
189
|
download_to_file(response, file_name)
|
|
134
190
|
# Return a read stream
|
|
135
191
|
result = ::File.open(file_name, "rb") { |io| builder.reader(io, &block) }
|
|
@@ -141,15 +197,46 @@ module IOStreams
|
|
|
141
197
|
|
|
142
198
|
# Validate that the host may be contacted, and that the scheme is still http(s)
|
|
143
199
|
# after following a redirect.
|
|
200
|
+
#
|
|
201
|
+
# A redirect must also be within the allowed paths, see `IOStreams.add_allowed_path`.
|
|
144
202
|
def validate_uri!(uri)
|
|
145
203
|
unless %w[http https].include?(uri.scheme)
|
|
146
|
-
raise(IOStreams::Errors::CommunicationsFailure,
|
|
204
|
+
raise(IOStreams::Errors::CommunicationsFailure,
|
|
205
|
+
"Invalid redirect, only http and https are supported: #{without_credentials(uri)}")
|
|
147
206
|
end
|
|
207
|
+
authorize_location!(http_location(uri)) unless IOStreams.allowed_paths.empty?
|
|
148
208
|
return if allow_hosts.nil? || allow_hosts.include?(uri.hostname)
|
|
149
209
|
|
|
150
210
|
raise(IOStreams::Errors::CommunicationsFailure, "Host not in the allowed list of hosts: #{uri.hostname}")
|
|
151
211
|
end
|
|
152
212
|
|
|
213
|
+
# Returns [String] the scheme, host, port and path of the url, which is compared against the allowed paths.
|
|
214
|
+
def allowed_location
|
|
215
|
+
http_location(original_uri)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Returns [String] the scheme, host, port and path of the supplied uri, without any credentials or query.
|
|
219
|
+
#
|
|
220
|
+
# The path is decoded and `.` and `..` resolved, the way most web servers resolve them, so that for
|
|
221
|
+
# example `%2e%2e` cannot be used to leave an allowed path. A backslash is treated as a `/`.
|
|
222
|
+
def http_location(uri)
|
|
223
|
+
raise(Errors::AccessDenied, "Access denied: #{without_credentials(uri)} has no host") if uri.host.to_s.empty?
|
|
224
|
+
|
|
225
|
+
path = URI.decode_uri_component(uri.path).tr("\\", "/")
|
|
226
|
+
"#{uri.scheme}://#{uri.host.downcase}:#{uri.port}#{normalize_path(path)}".chomp("/")
|
|
227
|
+
rescue ArgumentError => e
|
|
228
|
+
raise(Errors::AccessDenied, "Access denied to #{without_credentials(uri)}: #{e.message}")
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Returns [String] the uri without any user name or password, for use in error messages.
|
|
232
|
+
def without_credentials(uri)
|
|
233
|
+
return uri.to_s unless uri.user
|
|
234
|
+
|
|
235
|
+
uri = uri.dup
|
|
236
|
+
uri.user = nil
|
|
237
|
+
uri.to_s
|
|
238
|
+
end
|
|
239
|
+
|
|
153
240
|
def same_origin?(uri)
|
|
154
241
|
original = original_uri
|
|
155
242
|
uri.scheme == original.scheme && uri.hostname == original.hostname && uri.port == original.port
|
|
@@ -38,20 +38,23 @@ module IOStreams
|
|
|
38
38
|
|
|
39
39
|
private
|
|
40
40
|
|
|
41
|
+
# The directory is created without reporting a deprecation warning for its name, see `IOStreams::Paths.build`,
|
|
42
|
+
# since it is derived from the pattern, whose name `IOStreams.each_child` reports instead, and since other gems
|
|
43
|
+
# create a matcher themselves, such as Rocket Job, whose users cannot change its lines that a warning would name.
|
|
41
44
|
def extract_optimized_path(path, pattern)
|
|
42
45
|
elements = pattern.split("/")
|
|
43
46
|
index = elements.find_index { |e| e.match(MATCH_START_CHARS) }
|
|
44
47
|
if index.nil?
|
|
45
48
|
# No index means it has no pattern.
|
|
46
|
-
@path = path.nil? ?
|
|
49
|
+
@path = path.nil? ? Paths.build(pattern) : path.join(pattern)
|
|
47
50
|
@pattern = nil
|
|
48
51
|
elsif index.zero?
|
|
49
52
|
# Cannot optimize path since the very first entry contains a wildcard
|
|
50
|
-
@path = path ||
|
|
53
|
+
@path = path || Paths.build("")
|
|
51
54
|
@pattern = pattern
|
|
52
55
|
else
|
|
53
56
|
new_path = elements[0..(index - 1)].join("/")
|
|
54
|
-
@path = path.nil? ?
|
|
57
|
+
@path = path.nil? ? Paths.build(new_path) : path.join(new_path)
|
|
55
58
|
@pattern = elements[index..].join("/")
|
|
56
59
|
end
|
|
57
60
|
end
|
data/lib/io_streams/paths/s3.rb
CHANGED
|
@@ -5,6 +5,10 @@ module IOStreams
|
|
|
5
5
|
class S3 < IOStreams::Path
|
|
6
6
|
attr_reader :bucket_name, :options
|
|
7
7
|
|
|
8
|
+
# The secret access key, also within `client:`, and the customer encryption keys, see
|
|
9
|
+
# IOStreams::Path.redact_options. A session token and credentials are always sensitive by name.
|
|
10
|
+
def self.sensitive_option_names = %i[secret_access_key sse_customer_key copy_source_sse_customer_key]
|
|
11
|
+
|
|
8
12
|
# Largest file size supported by the S3 copy object api.
|
|
9
13
|
S3_COPY_OBJECT_SIZE_LIMIT = 5 * 1024 * 1024 * 1024
|
|
10
14
|
|
|
@@ -21,6 +25,15 @@ module IOStreams
|
|
|
21
25
|
# s3://my-bucket-name/file_name.txt
|
|
22
26
|
# s3://my-bucket-name/some_path/file_name.csv
|
|
23
27
|
#
|
|
28
|
+
# Any query string in the url is added to the S3 request parameters, for example:
|
|
29
|
+
# s3://my-bucket-name/file_name.csv?acl=bucket-owner-full-control
|
|
30
|
+
#
|
|
31
|
+
# SECURITY WARNING:
|
|
32
|
+
# Do not interpolate untrusted file names into the url, since a name such as
|
|
33
|
+
# `file.csv?acl=public-read` would set request parameters.
|
|
34
|
+
# Instead join untrusted names onto the path, which does not parse them as a query:
|
|
35
|
+
# IOStreams.path("s3://my-bucket-name/uploads").join(untrusted_name)
|
|
36
|
+
#
|
|
24
37
|
# access_key_id: [String]
|
|
25
38
|
# AWS Access Key Id to use to access this bucket.
|
|
26
39
|
#
|
|
@@ -169,14 +182,55 @@ module IOStreams
|
|
|
169
182
|
@client_options[:region] = region if region
|
|
170
183
|
end
|
|
171
184
|
|
|
172
|
-
@options = args
|
|
173
|
-
@options.merge!(uri.query.transform_keys(&:to_sym)) if uri.query
|
|
185
|
+
@options = uri.query ? args.merge(uri.query.transform_keys(&:to_sym)) : args
|
|
174
186
|
|
|
175
187
|
super(key)
|
|
188
|
+
# Each child that #each_child yields is a new path too, so nothing is checked while deprecation warnings are off.
|
|
189
|
+
return unless Warning[:deprecated]
|
|
190
|
+
|
|
191
|
+
warn_unusable_url(uri)
|
|
192
|
+
warn_client_arguments(client, access_key_id: access_key_id, secret_access_key: secret_access_key, region: region)
|
|
193
|
+
warn_unusable_options(args.keys) unless args.empty?
|
|
194
|
+
warn_unusable_query(uri.query) if uri.query
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# The S3 requests whose parameters IOStreams 3.0 takes as the options of a path, see .known_option_names.
|
|
198
|
+
OPTION_OPERATIONS = %i[get_object head_object put_object copy_object delete_object delete_objects list_objects_v2].freeze
|
|
199
|
+
private_constant :OPTION_OPERATIONS
|
|
200
|
+
|
|
201
|
+
# The parameters of the S3 requests that a path sets itself, such as `key`, which it overrides, and those of a
|
|
202
|
+
# listing that would change which keys it returns, or how, which IOStreams 3.0 does not take as the options of a
|
|
203
|
+
# path, see .known_option_names.
|
|
204
|
+
PATH_PARAMETERS = %i[
|
|
205
|
+
bucket key body response_target copy_source prefix continuation_token delimiter delete encoding_type start_after
|
|
206
|
+
].freeze
|
|
207
|
+
private_constant :PATH_PARAMETERS
|
|
208
|
+
|
|
209
|
+
# Returns [Array<Symbol>] the options of a path that IOStreams 3.0 takes, which raises ArgumentError for any other
|
|
210
|
+
# when the path is created: the parameters of its S3 requests, see OPTION_OPERATIONS, as the AWS SDK names them,
|
|
211
|
+
# other than those that the path sets itself, see PATH_PARAMETERS. Load the AWS SDK first.
|
|
212
|
+
def self.known_option_names
|
|
213
|
+
@known_option_names ||=
|
|
214
|
+
(OPTION_OPERATIONS.flat_map { |operation| ::Aws::S3::Client.api.operation(operation).input.shape.member_names }.uniq -
|
|
215
|
+
PATH_PARAMETERS).freeze
|
|
176
216
|
end
|
|
177
217
|
|
|
178
218
|
def to_s
|
|
179
|
-
::
|
|
219
|
+
Utils::PathName.join("s3://", bucket_name, path)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Joining a name that starts with `/` to the bucket itself, see `IOStreams::Path#join`, gives the key that starts
|
|
223
|
+
# with `/`, such as `/exports/a.csv`, while IOStreams v3.0 joins it as a key within the bucket, `exports/a.csv`, as
|
|
224
|
+
# a name joined to the local directory `/` is within it. So it is deprecated, and reports a deprecation warning,
|
|
225
|
+
# see `IOStreams::Deprecation`.
|
|
226
|
+
def join(*elements)
|
|
227
|
+
joined = super
|
|
228
|
+
if path.empty? && joined.path.start_with?("/")
|
|
229
|
+
Deprecation.warn("joining a name that starts with / to an S3 bucket is deprecated, since v3.0 joins it as " \
|
|
230
|
+
"a key within the bucket, such as exports/a.csv for /exports/a.csv: remove the leading /, " \
|
|
231
|
+
"or name a key that starts with / in the url, such as s3://bucket//exports/a.csv")
|
|
232
|
+
end
|
|
233
|
+
joined
|
|
180
234
|
end
|
|
181
235
|
|
|
182
236
|
# Does not support relative file names since there is no concept of current working directory
|
|
@@ -185,6 +239,7 @@ module IOStreams
|
|
|
185
239
|
end
|
|
186
240
|
|
|
187
241
|
def delete
|
|
242
|
+
authorize!
|
|
188
243
|
client.delete_object(bucket: bucket_name, key: path)
|
|
189
244
|
self
|
|
190
245
|
rescue Aws::S3::Errors::NotFound
|
|
@@ -192,6 +247,7 @@ module IOStreams
|
|
|
192
247
|
end
|
|
193
248
|
|
|
194
249
|
def exist?
|
|
250
|
+
authorize!
|
|
195
251
|
client.head_object(bucket: bucket_name, key: path)
|
|
196
252
|
true
|
|
197
253
|
rescue Aws::S3::Errors::NotFound
|
|
@@ -216,7 +272,10 @@ module IOStreams
|
|
|
216
272
|
target = IOStreams.new(target_path)
|
|
217
273
|
return super(target, convert: convert, **args) unless target.is_a?(self.class)
|
|
218
274
|
|
|
219
|
-
|
|
275
|
+
warn_unconverted_copy_options(args[:mode], args.except(:mode))
|
|
276
|
+
authorize!
|
|
277
|
+
target.authorize!
|
|
278
|
+
source_name = Utils::PathName.join(bucket_name, path)
|
|
220
279
|
client.copy_object(options.merge(bucket: target.bucket_name, key: target.path, copy_source: source_name))
|
|
221
280
|
target
|
|
222
281
|
end
|
|
@@ -230,7 +289,10 @@ module IOStreams
|
|
|
230
289
|
return super(source, convert: convert, **args)
|
|
231
290
|
end
|
|
232
291
|
|
|
233
|
-
|
|
292
|
+
warn_unconverted_copy_options(args[:mode], args.except(:mode))
|
|
293
|
+
authorize!
|
|
294
|
+
source.authorize!
|
|
295
|
+
source_name = Utils::PathName.join(source.bucket_name, source.path)
|
|
234
296
|
client.copy_object(options.merge(bucket: bucket_name, key: path, copy_source: source_name))
|
|
235
297
|
end
|
|
236
298
|
|
|
@@ -244,6 +306,7 @@ module IOStreams
|
|
|
244
306
|
end
|
|
245
307
|
|
|
246
308
|
def size
|
|
309
|
+
authorize!
|
|
247
310
|
client.head_object(bucket: bucket_name, key: path).content_length
|
|
248
311
|
rescue Aws::S3::Errors::NotFound
|
|
249
312
|
nil
|
|
@@ -254,7 +317,7 @@ module IOStreams
|
|
|
254
317
|
# Read from AWS S3 file.
|
|
255
318
|
def stream_reader(&block)
|
|
256
319
|
# Since S3 download only supports a push stream, write it to a tempfile first.
|
|
257
|
-
Utils.
|
|
320
|
+
Utils.private_temp_file("iostreams_s3") do |file_name|
|
|
258
321
|
read_file(file_name)
|
|
259
322
|
|
|
260
323
|
::File.open(file_name, "rb") { |io| builder.reader(io, &block) }
|
|
@@ -263,6 +326,7 @@ module IOStreams
|
|
|
263
326
|
|
|
264
327
|
# Shortcut method if caller has a filename already with no other streams applied:
|
|
265
328
|
def read_file(file_name)
|
|
329
|
+
authorize!
|
|
266
330
|
::File.open(file_name, "wb") do |file|
|
|
267
331
|
client.get_object(options.merge(response_target: file, bucket: bucket_name, key: path))
|
|
268
332
|
end
|
|
@@ -277,7 +341,7 @@ module IOStreams
|
|
|
277
341
|
# aborted.
|
|
278
342
|
def stream_writer(&block)
|
|
279
343
|
# Since S3 upload only supports a pull stream, write it to a tempfile first.
|
|
280
|
-
Utils.
|
|
344
|
+
Utils.private_temp_file("iostreams_s3") do |file_name|
|
|
281
345
|
result = ::File.open(file_name, "wb") { |io| builder.writer(io, &block) }
|
|
282
346
|
|
|
283
347
|
# Upload file only once all data has been written to it
|
|
@@ -288,6 +352,7 @@ module IOStreams
|
|
|
288
352
|
|
|
289
353
|
# Shortcut method if caller has a filename already with no other streams applied:
|
|
290
354
|
def write_file(file_name)
|
|
355
|
+
authorize!
|
|
291
356
|
if ::File.size(file_name) > MULTIPART_UPLOAD_SIZE
|
|
292
357
|
# Use multipart file upload
|
|
293
358
|
s3 = Aws::S3::Resource.new(client: client)
|
|
@@ -308,29 +373,26 @@ module IOStreams
|
|
|
308
373
|
case_sensitive: case_sensitive, directories: directories, hidden: hidden)
|
|
309
374
|
end
|
|
310
375
|
|
|
376
|
+
authorize!
|
|
311
377
|
matcher = Matcher.new(self, pattern, case_sensitive: case_sensitive, hidden: hidden)
|
|
312
378
|
|
|
313
379
|
# When the pattern includes an exact file name without any pattern characters
|
|
314
380
|
if matcher.pattern.nil?
|
|
315
|
-
yield(matcher.path) if matcher.path.exist?
|
|
381
|
+
yield(matcher.path) if allowed_child?(matcher.path) && matcher.path.exist?
|
|
316
382
|
return
|
|
317
383
|
end
|
|
318
384
|
|
|
319
385
|
prefix = Utils::URI.new(matcher.path.to_s).path.sub(%r{\A/}, "")
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
# Fetches upto 1,000 entries at a time
|
|
323
|
-
resp = client.list_objects_v2(bucket: bucket_name, prefix: prefix, continuation_token: token)
|
|
324
|
-
resp.contents.each do |object|
|
|
325
|
-
next if !directories && object.key.end_with?("/")
|
|
386
|
+
each_object(prefix) do |name, object|
|
|
387
|
+
next if !directories && object.key.end_with?("/")
|
|
326
388
|
|
|
327
|
-
|
|
328
|
-
|
|
389
|
+
file_name = Utils::PathName.join("s3://", name, object.key)
|
|
390
|
+
next unless matcher.match?(file_name)
|
|
329
391
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
392
|
+
child = child_path(name, object.key)
|
|
393
|
+
next unless allowed_child?(child)
|
|
394
|
+
|
|
395
|
+
yield(child, object.to_h)
|
|
334
396
|
end
|
|
335
397
|
nil
|
|
336
398
|
end
|
|
@@ -344,6 +406,166 @@ module IOStreams
|
|
|
344
406
|
def client
|
|
345
407
|
@client ||= ::Aws::S3::Client.new(@client_options)
|
|
346
408
|
end
|
|
409
|
+
|
|
410
|
+
private
|
|
411
|
+
|
|
412
|
+
# Reports a deprecation warning for each part of the url that this path cannot use, which IOStreams 3.0 raises
|
|
413
|
+
# ArgumentError for when the path is created, so that a mistake in a configured url is reported wherever the
|
|
414
|
+
# code runs: a url without a bucket, a fragment, which an unencoded `#` starts, and which is not part of the key, a
|
|
415
|
+
# port, and a user name or password, which are ignored.
|
|
416
|
+
def warn_unusable_url(uri)
|
|
417
|
+
warn_url("it names no bucket, name one, such as 's3://bucket/#{path}'") if bucket_name.to_s.empty?
|
|
418
|
+
if uri.fragment
|
|
419
|
+
warn_url("it has a fragment, which a '#' starts, and which is not part of the key, percent-encode a '#' in a " \
|
|
420
|
+
"key as '%23'")
|
|
421
|
+
end
|
|
422
|
+
if uri.port
|
|
423
|
+
warn_url("its port is ignored, supply the endpoint of another service that implements the S3 API with " \
|
|
424
|
+
"client: {endpoint: \"https://host:port\"}")
|
|
425
|
+
end
|
|
426
|
+
return unless uri.user || uri.password
|
|
427
|
+
|
|
428
|
+
warn_url("its user name and password are ignored, supply the credentials with access_key_id: and " \
|
|
429
|
+
"secret_access_key:")
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# Reports that the S3 url of this path is deprecated, since IOStreams 3.0 raises ArgumentError for it, for the
|
|
433
|
+
# supplied reason.
|
|
434
|
+
def warn_url(reason)
|
|
435
|
+
Deprecation.warn("the S3 url #{warning_url} is deprecated, since #{reason}")
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# Returns [String] the url of this path as a deprecation warning names it, quoted: its bucket and its key, without
|
|
439
|
+
# the parts of the url that the path cannot use, such as a user name, and with an empty bucket kept, unlike #to_s,
|
|
440
|
+
# such as `"s3:///a.csv"`.
|
|
441
|
+
def warning_url
|
|
442
|
+
Utils.display_text("s3://#{bucket_name}/#{path}").inspect
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# Reports a deprecation warning for the region and credentials supplied with an `Aws::S3::Client`, which ignores
|
|
446
|
+
# them, since it has its own, and which IOStreams 3.0 raises ArgumentError for when the path is created.
|
|
447
|
+
def warn_client_arguments(client, **arguments)
|
|
448
|
+
return if client.nil? || client.is_a?(Hash)
|
|
449
|
+
|
|
450
|
+
names = arguments.compact.keys
|
|
451
|
+
return if names.empty?
|
|
452
|
+
|
|
453
|
+
words = Deprecation::Words.new(names)
|
|
454
|
+
Deprecation.warn("#{words.keywords} #{words.are} deprecated with an Aws::S3::Client as client:, which ignores " \
|
|
455
|
+
"#{words.them}, since it has its own, supply #{words.them} to Aws::S3::Client.new instead, or " \
|
|
456
|
+
"supply client: as a Hash of its options")
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# Reports a deprecation warning for the options of this path that IOStreams 3.0 raises ArgumentError for when the
|
|
460
|
+
# path is created, see #unusable_option_names: one that the path sets itself, such as `key:`, which it overrides,
|
|
461
|
+
# and one that no S3 request takes, such as a misspelled one, which the AWS SDK raises for once the path is used.
|
|
462
|
+
def warn_unusable_options(names)
|
|
463
|
+
unusable_option_names(names).each_pair do |set_by_path, unusable|
|
|
464
|
+
words = Deprecation::Words.new(unusable)
|
|
465
|
+
warn_unusable("the S3 #{words.one? ? 'option' : 'options'} #{words.keywords}", words, set_by_path)
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
# Reports a deprecation warning for the names in the query of the url of this path that IOStreams 3.0 raises
|
|
470
|
+
# ArgumentError for when the path is created, as for an option, see #warn_unusable_options. A name without a
|
|
471
|
+
# value is more likely part of a key, such as the `?` of `s3://bucket/data/file?.csv`, so the warning then also
|
|
472
|
+
# says how to write a `?` in a key.
|
|
473
|
+
def warn_unusable_query(query)
|
|
474
|
+
unusable_option_names(query.keys.map(&:to_sym)).each_pair do |set_by_path, unusable|
|
|
475
|
+
names = unusable.map(&:to_s)
|
|
476
|
+
words = Deprecation::Words.new(names)
|
|
477
|
+
encode = ", and percent-encode a '?' in a key as '%3F'" if names.any? { |name| query[name].to_s.empty? }
|
|
478
|
+
warn_unusable("#{words.keywords} in the query of the S3 url #{warning_url}", words, set_by_path, encode)
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# Returns [Hash<true|false, Array>] the supplied names of options that IOStreams 3.0 raises ArgumentError for: by
|
|
483
|
+
# whether the path sets them itself (true), see PATH_PARAMETERS, or no S3 request of a path takes them (false),
|
|
484
|
+
# see .known_option_names, without either when there are none.
|
|
485
|
+
def unusable_option_names(names)
|
|
486
|
+
set_by_path = names & PATH_PARAMETERS
|
|
487
|
+
{true => set_by_path, false => names - set_by_path - self.class.known_option_names}.reject { |_, list| list.empty? }
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# Reports that the options, which the subject names, and the words are about, see `IOStreams::Deprecation::Words`,
|
|
491
|
+
# are deprecated, since the path sets them itself, or since no S3 request takes them, followed by the supplied
|
|
492
|
+
# advice.
|
|
493
|
+
def warn_unusable(subject, words, set_by_path, advice = nil)
|
|
494
|
+
them = words.them
|
|
495
|
+
reason =
|
|
496
|
+
if set_by_path
|
|
497
|
+
"the path sets #{them} itself, and overrides #{them}, so remove #{them}"
|
|
498
|
+
else
|
|
499
|
+
"no S3 request takes #{them}, so correct or remove #{them}"
|
|
500
|
+
end
|
|
501
|
+
Deprecation.warn("#{subject} #{words.are} deprecated, since #{reason}#{advice}")
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# Returns [Array(Array<Symbol>, String)|nil] the methods of the IO that v3.0 reads the object from, in place of the
|
|
505
|
+
# temp File that v2.2 downloads it into, and its name, see `IOStreams::Stream#v3_source`: v3.0 reads the object as
|
|
506
|
+
# the application reads it, with an IO that only reads, unless the `range` or `part_number` option chooses the
|
|
507
|
+
# bytes to read, which it still downloads into a temp file.
|
|
508
|
+
def v3_source
|
|
509
|
+
return if options.key?(:range) || options.key?(:part_number)
|
|
510
|
+
|
|
511
|
+
[IOStreams::Reader::V3_READ_METHODS, "the IO of an S3 path"]
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
# The methods of the IO that v3.0 uploads the object with, see #v3_target: those of an IO that write, and #flush.
|
|
515
|
+
V3_UPLOAD_METHODS = [*IOStreams::Writer::V3_WRITE_METHODS, :flush].freeze
|
|
516
|
+
private_constant :V3_UPLOAD_METHODS
|
|
517
|
+
|
|
518
|
+
# The options that describe the whole object, with which v3.0 still writes the object into a temp file first, as
|
|
519
|
+
# does a checksum of its data, such as `checksum_crc32`, see #v3_target.
|
|
520
|
+
V3_WHOLE_OBJECT_OPTIONS = %i[content_length content_md5 write_offset_bytes].freeze
|
|
521
|
+
private_constant :V3_WHOLE_OBJECT_OPTIONS
|
|
522
|
+
|
|
523
|
+
# Returns [Array(Array<Symbol>, String)|nil] the methods of the IO that v3.0 uploads the object with, in place of the
|
|
524
|
+
# temp File that v2.2 writes it into, and its name, see `IOStreams::Stream#v3_target`: v3.0 uploads the object as
|
|
525
|
+
# the application writes it, with an IO that only writes, unless an option describes the whole object, which it
|
|
526
|
+
# still writes into a temp file first. v3.0 writes the object with only the options that `put_object` takes, so
|
|
527
|
+
# only those decide, rather than one that only another request takes, such as `checksum_mode` when reading.
|
|
528
|
+
def v3_target
|
|
529
|
+
put_object = ::Aws::S3::Client.api.operation(:put_object).input.shape
|
|
530
|
+
whole_object = options.each_key.any? do |name|
|
|
531
|
+
put_object.member?(name) &&
|
|
532
|
+
(V3_WHOLE_OBJECT_OPTIONS.include?(name) || (name.start_with?("checksum_") && name != :checksum_algorithm))
|
|
533
|
+
end
|
|
534
|
+
[V3_UPLOAD_METHODS, "the IO of an S3 path"] unless whole_object
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
# Yields the bucket name and each object in the bucket whose key starts with the supplied prefix.
|
|
538
|
+
def each_object(prefix)
|
|
539
|
+
token = nil
|
|
540
|
+
loop do
|
|
541
|
+
# Fetches upto 1,000 entries at a time
|
|
542
|
+
resp = client.list_objects_v2(bucket: bucket_name, prefix: prefix, continuation_token: token)
|
|
543
|
+
resp.contents.each { |object| yield(resp.name, object) }
|
|
544
|
+
token = resp.next_continuation_token
|
|
545
|
+
break if token.nil?
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
# Returns [String] the bucket and key, which is compared against the allowed paths.
|
|
550
|
+
#
|
|
551
|
+
# S3 treats `.` and `..` in a key as ordinary characters, but other services that implement the
|
|
552
|
+
# S3 API may resolve them, so keys containing them are denied.
|
|
553
|
+
def allowed_location
|
|
554
|
+
if path.split("/").intersect?([".", ".."])
|
|
555
|
+
raise(Errors::AccessDenied, "Access denied to #{self}: '.' and '..' are not allowed in S3 keys")
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
to_s.sub(%r{/+\z}, "")
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
# Set the key directly rather than parsing it as part of a URL, since a key can contain
|
|
562
|
+
# characters such as `?`, `+` or `%` that a URL parser would treat as a query or as escapes.
|
|
563
|
+
#
|
|
564
|
+
# The child uses this path's client, so that it has the same credentials and region.
|
|
565
|
+
def child_path(bucket_name, key)
|
|
566
|
+
child = self.class.new("s3://#{bucket_name}", client: client)
|
|
567
|
+
child.replace_path(key.dup.freeze)
|
|
568
|
+
end
|
|
347
569
|
end
|
|
348
570
|
end
|
|
349
571
|
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
module IOStreams
|
|
2
|
+
module Paths
|
|
3
|
+
class SFTP < IOStreams::Path
|
|
4
|
+
# Translates the ssh options supplied for the sftp executable into options for net-ssh,
|
|
5
|
+
# which `SFTP#each_child` uses to list files instead of the sftp executable.
|
|
6
|
+
#
|
|
7
|
+
# The net-ssh options match those that SFTP passes to the sftp executable.
|
|
8
|
+
module NetSSH
|
|
9
|
+
# The ssh options that net-ssh supports.
|
|
10
|
+
OPTIONS = %w[
|
|
11
|
+
HostKey IdentityKey IdentityFile UserKnownHostsFile StrictHostKeyChecking
|
|
12
|
+
ConnectTimeout ServerAliveInterval ServerAliveCountMax LogLevel
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
# Yields [Hash] the net-ssh options.
|
|
16
|
+
#
|
|
17
|
+
# Raises [ArgumentError] when `ssh_options` includes an option that is not in `OPTIONS`.
|
|
18
|
+
def self.options(ssh_options, port:, password:)
|
|
19
|
+
options = {port: port, max_pkt_size: 65_536, non_interactive: true}
|
|
20
|
+
options[:logger] = IOStreams.logger if IOStreams.logger
|
|
21
|
+
# Like the sftp executable, only use the password when one is supplied, and otherwise only public keys.
|
|
22
|
+
if password
|
|
23
|
+
options[:password] = password
|
|
24
|
+
options[:auth_methods] = %w[password keyboard-interactive]
|
|
25
|
+
else
|
|
26
|
+
options[:auth_methods] = %w[publickey]
|
|
27
|
+
end
|
|
28
|
+
# Like the sftp executable, which uses `StrictHostKeyChecking=yes`, instead of the
|
|
29
|
+
# net-ssh default of trusting a host key the first time it is seen.
|
|
30
|
+
options[:verify_host_key] = :always
|
|
31
|
+
|
|
32
|
+
ssh_options.each_pair { |key, value| add(options, key, value) }
|
|
33
|
+
return yield(options) unless ssh_options.key?("HostKey")
|
|
34
|
+
|
|
35
|
+
# Like the sftp executable, the host key replaces the user's known_hosts file.
|
|
36
|
+
Utils.private_temp_file("iostreams-sftp-known-hosts") do |file_name|
|
|
37
|
+
::File.binwrite(file_name, ssh_options["HostKey"])
|
|
38
|
+
options[:user_known_hosts_file] = [file_name]
|
|
39
|
+
yield(options)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.add(options, key, value)
|
|
44
|
+
case key
|
|
45
|
+
when "HostKey"
|
|
46
|
+
# Written to a temp file by `.options`.
|
|
47
|
+
when "IdentityKey"
|
|
48
|
+
(options[:key_data] ||= []) << value
|
|
49
|
+
options[:keys_only] = true
|
|
50
|
+
when "IdentityFile"
|
|
51
|
+
(options[:keys] ||= []) << value
|
|
52
|
+
options[:keys_only] = true
|
|
53
|
+
when "UserKnownHostsFile"
|
|
54
|
+
options[:user_known_hosts_file] = value.to_s.split
|
|
55
|
+
when "StrictHostKeyChecking"
|
|
56
|
+
options[:verify_host_key] = verify_host_key(value)
|
|
57
|
+
when "ConnectTimeout"
|
|
58
|
+
options[:timeout] = Integer(value)
|
|
59
|
+
when "ServerAliveInterval"
|
|
60
|
+
options[:keepalive] = Integer(value).positive?
|
|
61
|
+
options[:keepalive_interval] = Integer(value)
|
|
62
|
+
when "ServerAliveCountMax"
|
|
63
|
+
options[:keepalive_maxcount] = Integer(value)
|
|
64
|
+
when "LogLevel"
|
|
65
|
+
options[:verbose] = log_level(value)
|
|
66
|
+
else
|
|
67
|
+
raise(ArgumentError,
|
|
68
|
+
"SFTP #each_child does not support the ssh option #{key.inspect}. It supports: #{OPTIONS.join(', ')}")
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.verify_host_key(value)
|
|
73
|
+
case value.to_s.downcase
|
|
74
|
+
when "yes", "ask"
|
|
75
|
+
:always
|
|
76
|
+
when "accept-new"
|
|
77
|
+
:accept_new
|
|
78
|
+
when "no", "off"
|
|
79
|
+
:never
|
|
80
|
+
else
|
|
81
|
+
raise(ArgumentError, "Invalid StrictHostKeyChecking value: #{value.inspect}")
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.log_level(value)
|
|
86
|
+
case value.to_s.upcase
|
|
87
|
+
when "QUIET", "FATAL"
|
|
88
|
+
:fatal
|
|
89
|
+
when "ERROR"
|
|
90
|
+
:error
|
|
91
|
+
when "INFO", "VERBOSE"
|
|
92
|
+
:info
|
|
93
|
+
when "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3"
|
|
94
|
+
:debug
|
|
95
|
+
else
|
|
96
|
+
raise(ArgumentError, "Invalid LogLevel value: #{value.inspect}")
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private_class_method :add, :verify_host_key, :log_level
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|