forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_util.py
301 lines (268 loc) · 11 KB
/
s3_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from __future__ import division
import errno
import os
from collections import namedtuple
from logging import getLogger
import logging
import OpenSSL
import boto3
import botocore.exceptions
from boto3.exceptions import RetriesExceededError, S3UploadFailedError
from boto3.s3.transfer import TransferConfig
from botocore.client import Config
from .compat import TO_UNICODE
from .constants import (SHA256_DIGEST, ResultStatus, FileHeader)
from .encryption_util import (EncryptionMetadata)
SFC_DIGEST = u'sfc-digest'
AMZ_MATDESC = u"x-amz-matdesc"
AMZ_KEY = u"x-amz-key"
AMZ_IV = u"x-amz-iv"
ERRORNO_WSAECONNABORTED = 10053 # network connection was aborted
EXPIRED_TOKEN = u'ExpiredToken'
"""
S3 Location: S3 bucket name + path
"""
S3Location = namedtuple(
"S3Location", [
"bucket_name", # S3 bucket name
"s3path" # S3 path name
])
class SnowflakeS3Util:
"""
S3 Utility class
"""
# magic number, given from the AWS error message.
DATA_SIZE_THRESHOLD = 5242880
@staticmethod
def create_client(stage_info, use_accelerate_endpoint=False):
"""
Creates a client object with a stage credential
:param stage_credentials: a stage credential
:param use_accelerate_endpoint: is accelerate endpoint?
:return: client
"""
logger = getLogger(__name__)
stage_credentials = stage_info[u'creds']
security_token = stage_credentials.get(u'AWS_TOKEN', None)
logger.debug(u"AWS_ID: %s", stage_credentials[u'AWS_ID'])
config = Config(
signature_version=u's3v4',
s3={
'use_accelerate_endpoint': use_accelerate_endpoint,
})
client = boto3.resource(
u's3',
region_name=stage_info['region'],
aws_access_key_id=stage_credentials[u'AWS_ID'],
aws_secret_access_key=stage_credentials[u'AWS_KEY'],
aws_session_token=security_token,
config=config,
)
return client
@staticmethod
def extract_bucket_name_and_path(stage_location):
stage_location = os.path.expanduser(stage_location)
bucket_name = stage_location
s3path = u''
# split stage location as bucket name and path
if u'/' in stage_location:
bucket_name = stage_location[0:stage_location.index(u'/')]
s3path = stage_location[stage_location.index(u'/') + 1:]
if s3path and not s3path.endswith(u'/'):
s3path += u'/'
return S3Location(
bucket_name=bucket_name,
s3path=s3path)
@staticmethod
def _get_s3_object(meta, filename):
logger = getLogger(__name__)
client = meta[u'client']
s3location = SnowflakeS3Util.extract_bucket_name_and_path(
meta[u'stage_info'][u'location'])
s3path = s3location.s3path + filename.lstrip('/')
if logger.getEffectiveLevel() == logging.DEBUG:
tmp_meta = {}
for k, v in meta.items():
if k != 'stage_credentials':
tmp_meta[k] = v
logger.debug(
u"s3location.bucket_name: %s, "
u"s3location.s3path: %s, "
u"s3fullpath: %s, "
u'meta: %s',
s3location.bucket_name,
s3location.s3path,
s3path, tmp_meta)
return client.Object(s3location.bucket_name, s3path)
@staticmethod
def get_file_header(meta, filename):
"""
Gets S3 file object
:param meta: file meta object
:return: S3 object if no error, otherwise None. Check meta[
u'result_status'] for status.
"""
logger = getLogger(__name__)
akey = SnowflakeS3Util._get_s3_object(meta, filename)
try:
# HTTP HEAD request
akey.load()
except botocore.exceptions.ClientError as e:
if e.response[u'Error'][u'Code'] == EXPIRED_TOKEN:
logger.debug(u"AWS Token expired. Renew and retry")
meta[u'result_status'] = ResultStatus.RENEW_TOKEN
return None
elif e.response[u'Error'][u'Code'] == u'404':
logger.debug(u'not found. bucket: %s, path: %s',
akey.bucket_name, akey.key)
meta[u'result_status'] = ResultStatus.NOT_FOUND_FILE
return FileHeader(
digest=None,
content_length=None,
encryption_metadata=None,
)
elif e.response[u'Error'][u'Code'] == u'400':
logger.debug(u'Bad request, token needs to be renewed: %s. '
u'bucket: %s, path: %s',
e.response[u'Error'][u'Message'],
akey.bucket_name, akey.key)
meta[u'result_status'] = ResultStatus.RENEW_TOKEN
return None
logger.debug(
u"Failed to get metadata for %s, %s: %s",
akey.bucket_name, akey.key, e)
meta[u'result_status'] = ResultStatus.ERROR
return None
meta[u'result_status'] = ResultStatus.UPLOADED
encryption_metadata = EncryptionMetadata(
key=akey.metadata.get(AMZ_KEY),
iv=akey.metadata.get(AMZ_IV),
matdesc=akey.metadata.get(AMZ_MATDESC),
) if akey.metadata.get(AMZ_KEY) else None
return FileHeader(
digest=akey.metadata.get(SFC_DIGEST),
content_length=akey.content_length,
encryption_metadata=encryption_metadata
)
@staticmethod
def upload_file(data_file, meta, encryption_metadata, max_concurrency):
logger = getLogger(__name__)
try:
s3_metadata = {
u'Content-Type': u'application/octet-stream',
SFC_DIGEST: meta[SHA256_DIGEST],
}
if (encryption_metadata):
s3_metadata.update({
AMZ_IV: encryption_metadata.iv,
AMZ_KEY: encryption_metadata.key,
AMZ_MATDESC: encryption_metadata.matdesc,
})
s3location = SnowflakeS3Util.extract_bucket_name_and_path(
meta[u'stage_info'][u'location'])
s3path = s3location.s3path + meta[u'dst_file_name'].lstrip('/')
akey = meta[u'client'].Object(s3location.bucket_name, s3path)
akey.upload_file(
data_file,
Callback=meta[u'put_callback'](
data_file,
os.path.getsize(data_file),
output_stream=meta[u'put_callback_output_stream']) if
meta[u'put_callback'] else None,
ExtraArgs={
u'Metadata': s3_metadata,
u'ContentEncoding': u'gzip',
},
Config=TransferConfig(
multipart_threshold=SnowflakeS3Util.DATA_SIZE_THRESHOLD,
max_concurrency=max_concurrency,
num_download_attempts=10,
)
)
logger.debug(u'DONE putting a file')
meta[u'dst_file_size'] = meta[u'upload_size']
meta[u'result_status'] = ResultStatus.UPLOADED
except botocore.exceptions.ClientError as err:
if err.response[u'Error'][u'Code'] == EXPIRED_TOKEN:
logger.debug(u"AWS Token expired. Renew and retry")
meta[u'result_status'] = ResultStatus.RENEW_TOKEN
return
logger.debug(
u"Failed to upload a file: %s, err: %s",
data_file, err, exc_info=True)
raise err
except S3UploadFailedError as err:
if EXPIRED_TOKEN in TO_UNICODE(err):
# Since AWS token expiration error can be encapsulated in
# S3UploadFailedError, the text match is required to
# identify the case.
logger.debug(
'Failed to upload a file: %s, err: %s. Renewing '
'AWS Token and Retrying',
data_file, err)
meta[u'result_status'] = ResultStatus.RENEW_TOKEN
return
meta[u'last_error'] = err
meta[u'result_status'] = ResultStatus.NEED_RETRY
except OpenSSL.SSL.SysCallError as err:
if err.args[0] not in (
ERRORNO_WSAECONNABORTED,
errno.ECONNRESET,
errno.ETIMEDOUT,
errno.EPIPE,
-1):
raise err
meta[u'last_error'] = err
if err.args[0] == ERRORNO_WSAECONNABORTED:
# connection was disconnected by S3
# because of too many connections. retry with
# less concurrency to mitigate it
meta[u'result_status'] = ResultStatus.NEED_RETRY_WITH_LOWER_CONCURRENCY
else:
meta[u'result_status'] = ResultStatus.NEED_RETRY
@staticmethod
def _native_download_file(meta, full_dst_file_name, max_concurrency):
logger = getLogger(__name__)
try:
akey = SnowflakeS3Util._get_s3_object(meta, meta[u'src_file_name'])
akey.download_file(
full_dst_file_name,
Callback=meta[u'get_callback'](
meta[u'src_file_name'],
meta[u'src_file_size'],
output_stream=meta[u'get_callback_output_stream']) if
meta[u'get_callback'] else None,
Config=TransferConfig(
multipart_threshold=SnowflakeS3Util.DATA_SIZE_THRESHOLD,
max_concurrency=max_concurrency,
num_download_attempts=10,
)
)
meta[u'result_status'] = ResultStatus.DOWNLOADED
except botocore.exceptions.ClientError as err:
if err.response[u'Error'][u'Code'] == EXPIRED_TOKEN:
meta[u'result_status'] = ResultStatus.RENEW_TOKEN
else:
logger.debug(
u"Failed to download a file: %s, err: %s",
full_dst_file_name, err, exc_info=True)
raise err
except RetriesExceededError as err:
meta[u'result_status'] = ResultStatus.NEED_RETRY
meta[u'last_error'] = err
except OpenSSL.SSL.SysCallError as err:
if err.args[0] not in (
ERRORNO_WSAECONNABORTED,
errno.ECONNRESET,
errno.ETIMEDOUT,
errno.EPIPE,
-1):
raise err
meta[u'last_error'] = err
if err.args[0] == ERRORNO_WSAECONNABORTED:
# connection was disconnected by S3
# because of too many connections. retry with
# less concurrency to mitigate it
meta[u'result_status'] = ResultStatus.NEED_RETRY_WITH_LOWER_CONCURRENCY
else:
meta[u'result_status'] = ResultStatus.NEED_RETRY