|
1
|
|
|
""" |
|
2
|
|
|
Copyright George Sibble 2018 |
|
3
|
|
|
""" |
|
4
|
1 |
|
import requests |
|
5
|
1 |
|
import datetime |
|
|
|
|
|
|
6
|
1 |
|
from requests.auth import HTTPBasicAuth |
|
7
|
1 |
|
from b2blaze.b2_exceptions import B2Exception, B2AuthorizationError, B2InvalidRequestType |
|
8
|
1 |
|
import sys |
|
|
|
|
|
|
9
|
1 |
|
from hashlib import sha1 |
|
|
|
|
|
|
10
|
1 |
|
from b2blaze.utilities import b2_url_encode, decode_error, get_content_length, StreamWithHashProgress |
|
|
|
|
|
|
11
|
1 |
|
from .api import BASE_URL, API_VERSION, API |
|
12
|
|
|
|
|
13
|
1 |
|
class B2Connector(object): |
|
|
|
|
|
|
14
|
|
|
""" |
|
15
|
|
|
|
|
16
|
|
|
""" |
|
17
|
1 |
|
def __init__(self, key_id, application_key): |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
:param key_id: |
|
21
|
|
|
:param application_key: |
|
22
|
|
|
""" |
|
23
|
1 |
|
self.key_id = key_id |
|
24
|
1 |
|
self.application_key = application_key |
|
25
|
1 |
|
self.account_id = None |
|
26
|
1 |
|
self.auth_token = None |
|
27
|
1 |
|
self.authorized_at = None |
|
28
|
1 |
|
self.api_url = None |
|
29
|
1 |
|
self.download_url = None |
|
30
|
1 |
|
self.recommended_part_size = None |
|
31
|
1 |
|
self.api_session = None |
|
32
|
1 |
|
self.bucket_id = None |
|
33
|
|
|
#TODO: Part Size |
|
|
|
|
|
|
34
|
1 |
|
self._authorize() |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
1 |
|
@property |
|
38
|
|
|
def authorized(self): |
|
39
|
|
|
""" |
|
40
|
|
|
|
|
41
|
|
|
:return: |
|
42
|
|
|
""" |
|
43
|
|
|
if self.auth_token is None: |
|
44
|
|
|
return False |
|
45
|
|
|
else: |
|
46
|
|
|
if (datetime.datetime.utcnow() - self.authorized_at) > datetime.timedelta(hours=23): |
|
47
|
|
|
self._authorize() |
|
48
|
|
|
return True |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
1 |
|
def _authorize(self): |
|
52
|
|
|
""" |
|
53
|
|
|
|
|
54
|
|
|
:return: |
|
55
|
|
|
""" |
|
56
|
1 |
|
path = BASE_URL + API.authorize |
|
57
|
|
|
|
|
58
|
1 |
|
result = requests.get(path, auth=HTTPBasicAuth(self.key_id, self.application_key)) |
|
59
|
1 |
|
if result.status_code == 200: |
|
60
|
|
|
result_json = result.json() |
|
61
|
|
|
self.authorized_at = datetime.datetime.utcnow() |
|
62
|
|
|
self.account_id = result_json['accountId'] |
|
63
|
|
|
self.auth_token = result_json['authorizationToken'] |
|
64
|
|
|
self.api_url = result_json['apiUrl'] + API_VERSION |
|
65
|
|
|
self.download_url = result_json['downloadUrl'] + API_VERSION + API.download_file_by_id |
|
66
|
|
|
self.recommended_part_size = result_json['recommendedPartSize'] |
|
67
|
|
|
if result_json.get('allowed'): |
|
68
|
|
|
self.bucket_id = result_json['allowed'].get('bucketId', None) |
|
69
|
|
|
self.api_session = requests.Session() |
|
70
|
|
|
self.api_session.headers.update({ |
|
71
|
|
|
'Authorization': self.auth_token |
|
72
|
|
|
}) |
|
73
|
|
|
else: |
|
74
|
1 |
|
raise B2Exception.parse(result) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
def make_request(self, path, method='get', headers={}, params={}, account_id_required=False): |
|
|
|
|
|
|
78
|
|
|
""" |
|
79
|
|
|
|
|
80
|
|
|
:param path: |
|
81
|
|
|
:param method: |
|
82
|
|
|
:param headers: |
|
83
|
|
|
:param params: |
|
84
|
|
|
:param account_id_required: |
|
85
|
|
|
:return: |
|
86
|
|
|
""" |
|
87
|
|
|
if self.authorized: |
|
88
|
|
|
url = self.api_url + path |
|
89
|
|
|
if method == 'get': |
|
90
|
|
|
return self.api_session.get(url, headers=headers) |
|
91
|
|
|
elif method == 'post': |
|
92
|
|
|
if account_id_required: |
|
93
|
|
|
params.update({ |
|
94
|
|
|
'accountId': self.account_id |
|
95
|
|
|
}) |
|
96
|
|
|
headers.update({ |
|
97
|
|
|
'Content-Type': 'application/json' |
|
98
|
|
|
}) |
|
99
|
|
|
return self.api_session.post(url, json=params, headers=headers) |
|
100
|
|
|
else: |
|
101
|
|
|
raise B2InvalidRequestType('Request type must be get or post') |
|
102
|
|
|
else: |
|
103
|
|
|
raise B2AuthorizationError('Unknown Error') |
|
104
|
|
|
|
|
105
|
1 |
|
def upload_file(self, file_contents, file_name, upload_url, auth_token, |
|
|
|
|
|
|
106
|
|
|
direct=False, mime_content_type=None, content_length=None, progress_listener=None): |
|
|
|
|
|
|
107
|
|
|
""" |
|
108
|
|
|
|
|
109
|
|
|
:param file_contents: |
|
110
|
|
|
:param file_name: |
|
111
|
|
|
:param upload_url: |
|
112
|
|
|
:param auth_token: |
|
113
|
|
|
:param mime_content_type: |
|
114
|
|
|
:param content_length |
|
115
|
|
|
:param progress_listener |
|
116
|
|
|
:return: |
|
117
|
|
|
""" |
|
118
|
|
|
if hasattr(file_contents, 'read'): |
|
119
|
|
|
if content_length is None: |
|
120
|
|
|
content_length = get_content_length(file_contents) |
|
121
|
|
|
file_sha = 'hex_digits_at_end' |
|
122
|
|
|
data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener) |
|
123
|
|
|
content_length += data.hash_size() |
|
124
|
|
|
else: |
|
125
|
|
|
if content_length is None: |
|
126
|
|
|
content_length = len(file_contents) |
|
127
|
|
|
file_sha = sha1(file_contents).hexdigest() |
|
128
|
|
|
data = file_contents |
|
129
|
|
|
|
|
130
|
|
|
headers = { |
|
131
|
|
|
'Content-Type': mime_content_type or 'b2/x-auto', |
|
132
|
|
|
'Content-Length': str(content_length), |
|
133
|
|
|
'X-Bz-Content-Sha1': file_sha, |
|
134
|
|
|
'X-Bz-File-Name': b2_url_encode(file_name), |
|
135
|
|
|
'Authorization': auth_token |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return requests.post(upload_url, headers=headers, data=data) |
|
139
|
|
|
|
|
140
|
1 |
|
def upload_part(self, file_contents, content_length, part_number, upload_url, auth_token, progress_listener=None): |
|
|
|
|
|
|
141
|
|
|
""" |
|
142
|
|
|
|
|
143
|
|
|
:param file_contents: |
|
144
|
|
|
:param content_length: |
|
145
|
|
|
:param part_number: |
|
146
|
|
|
:param upload_url: |
|
147
|
|
|
:param auth_token: |
|
148
|
|
|
:param progress_listener: |
|
149
|
|
|
:return: |
|
150
|
|
|
""" |
|
151
|
|
|
file_sha = 'hex_digits_at_end' |
|
152
|
|
|
data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener) |
|
153
|
|
|
content_length += data.hash_size() |
|
154
|
|
|
|
|
155
|
|
|
headers = { |
|
156
|
|
|
'Content-Length': str(content_length), |
|
157
|
|
|
'X-Bz-Content-Sha1': file_sha, |
|
158
|
|
|
'X-Bz-Part-Number': str(part_number), |
|
159
|
|
|
'Authorization': auth_token |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return requests.post(upload_url, headers=headers, data=data) |
|
163
|
|
|
|
|
164
|
1 |
|
def download_file(self, file_id): |
|
165
|
|
|
""" |
|
166
|
|
|
|
|
167
|
|
|
:param file_id: |
|
168
|
|
|
:return: |
|
169
|
|
|
""" |
|
170
|
|
|
url = self.download_url |
|
171
|
|
|
params = { |
|
172
|
|
|
'fileId': file_id |
|
173
|
|
|
} |
|
174
|
|
|
headers = { |
|
175
|
|
|
'Authorization': self.auth_token |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return requests.get(url, headers=headers, params=params) |
|
179
|
|
|
|
|
|
|
|
|
|
180
|
|
|
|