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