This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
s3_uploader.py
194 lines (153 loc) · 6.18 KB
/
s3_uploader.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
# ------------------------------------------------------------------------------
# Copyright (c) 2015-2019 Digital Sapphire
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
import logging
import os
import sys
import threading
from boto3.session import Session
try:
from pyupdater.core.uploader import BaseUploader
except ImportError: # PyUpdater <3.0
from pyupdater.uploader import BaseUploader
from pyupdater.utils.exceptions import UploaderError
log = logging.getLogger(__name__)
class S3Uploader(BaseUploader):
name = 'S3'
author = 'Digital Sapphire'
def init_config(self, config):
self.access_key = os.environ.get(u'PYU_AWS_ID')
if self.access_key is None:
raise UploaderError('Missing PYU_AWS_ID',
expected=True)
self.secret_key = os.environ.get(u'PYU_AWS_SECRET')
if self.secret_key is None:
raise UploaderError(u'Missing PYU_AWS_SECRET',
expected=True)
self.session_token = os.environ.get(u'PYU_AWS_SESSION_TOKEN')
# Try to get bucket from env var
self.bucket_name = os.environ.get(u'PYU_AWS_BUCKET')
bucket_name = config.get(u'bucket_name')
# If there is a bucket name in the repo config we
# override the env var
if bucket_name is not None:
self.bucket_name = bucket_name
# If nothing is set throw an error
if self.bucket_name is None:
raise UploaderError(u'Bucket name is not set',
expected=True)
# Try to get bucket key from env var
self.bucket_key = os.environ.get(u'PYU_AWS_BUCKET_KEY')
bucket_key = config.get(u'bucket_key')
# If there is a bucket key in the repo config we
# override the env var
if bucket_key is not None:
self.bucket_key = bucket_key
# If nothing is set default to bucket root
if self.bucket_key is None:
self.bucket_key = ''
# Strip slashes for sanity
self.bucket_key = self.bucket_key.rstrip('/\\').lstrip('/\\')
# Try to get bucket region
self.bucket_region = os.environ.get(u'PYU_AWS_BUCKET_REGION')
bucket_region = config.get(u'bucket_region')
# If there is a bucket key in the repo config we
# override the env var
if bucket_region is not None:
self.bucket_region = bucket_region
# If nothing is set default to bucket root
if self.bucket_region is None:
self.bucket_region = 'us-west-2'
self._connect()
def _connect(self):
session = Session(
aws_access_key_id=self.access_key,
aws_secret_access_key=self.secret_key,
aws_session_token=self.session_token,
region_name=self.bucket_region
)
self.s3 = session.client('s3')
def set_config(self, config):
bucket_name = config.get('bucket_name')
bucket_name = self.get_answer(
'Please enter a bucket name',
default=bucket_name
)
config['bucket_name'] = bucket_name
bucket_key = config.get('bucket_key')
bucket_key = self.get_answer(
'Please enter a bucket key',
default=bucket_key
)
config['bucket_key'] = bucket_key
bucket_region = config.get('bucket_region')
bucket_region = self.get_answer(
'Please enter a bucket region',
default=bucket_region
)
config['bucket_region'] = bucket_region
def upload_file(self, filename):
"""Uploads a single file to S3
Args:
filename (str): Name of file to upload.
Returns:
(bool) Meanings::
True - Upload Successful
False - Upload Failed
"""
if not self.bucket_key:
key = os.path.basename(filename)
else:
key = self.bucket_key + '/' + os.path.basename(filename)
try:
self.s3.upload_file(
filename,
self.bucket_name,
key,
ExtraArgs={'ACL': 'public-read'},
Callback=ProgressPercentage(filename)
)
log.debug('Uploaded {}'.format(filename))
return True
except Exception as err:
log.error('Failed to upload file')
log.debug(err, exc_info=True)
self._connect()
return False
class ProgressPercentage(object):
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify we'll assume this is hooked up
# to a single filename.
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write(
"\r%s / %s (%.2f%%)" % (self._seen_so_far,
self._size, percentage))
sys.stdout.flush()