-
Notifications
You must be signed in to change notification settings - Fork 61
/
api.py
534 lines (451 loc) · 20 KB
/
api.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
######################################################################
#
# File: b2sdk/api.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from typing import Any, Dict, Optional
from .bucket import Bucket, BucketFactory
from .encryption.setting import EncryptionSetting
from .exception import NonExistentBucket, RestrictedBucket
from .file_lock import FileRetentionSetting, LegalHold
from .file_version import FileIdAndName, FileVersionFactory
from .large_file.services import LargeFileServices
from .raw_api import API_VERSION
from .session import B2Session
from .transfer import (
CopyManager,
DownloadManager,
Emerger,
UploadManager,
)
from .utils import B2TraceMeta, b2_url_encode, limit_trace_arguments
def url_for_api(info, api_name):
"""
Return URL for an API endpoint.
:param info: account info
:param str api_nam:
:rtype: str
"""
if api_name in ['b2_download_file_by_id']:
base = info.get_download_url()
else:
base = info.get_api_url()
return '%s/b2api/%s/%s' % (base, API_VERSION, api_name)
class Services(object):
""" Gathers objects that provide high level logic over raw api usage. """
def __init__(self, api, max_upload_workers=10, max_copy_workers=10):
"""
Initialize Services object using given session.
:param b2sdk.v1.B2Api api:
:param int max_upload_workers: a number of upload threads
:param int max_copy_workers: a number of copy threads
"""
self.api = api
self.session = api.session
self.large_file = LargeFileServices(self)
self.download_manager = DownloadManager(self)
self.upload_manager = UploadManager(self, max_upload_workers=max_upload_workers)
self.copy_manager = CopyManager(self, max_copy_workers=max_copy_workers)
self.emerger = Emerger(self)
class B2Api(metaclass=B2TraceMeta):
"""
Provide file-level access to B2 services.
While :class:`b2sdk.v1.B2RawApi` provides direct access to the B2 web APIs, this
class handles several things that simplify the task of uploading
and downloading files:
- re-acquires authorization tokens when they expire
- retrying uploads when an upload URL is busy
- breaking large files into parts
- emulating a directory structure (B2 buckets are flat)
Adds an object-oriented layer on top of the raw API, so that
buckets and files returned are Python objects with accessor
methods.
The class also keeps a cache of information needed to access the
service, such as auth tokens and upload URLs.
"""
BUCKET_FACTORY_CLASS = staticmethod(BucketFactory)
BUCKET_CLASS = staticmethod(Bucket)
SESSION_CLASS = staticmethod(B2Session)
FILE_VERSION_FACTORY_CLASS = staticmethod(FileVersionFactory)
def __init__(
self,
account_info=None,
cache=None,
raw_api=None,
max_upload_workers=10,
max_copy_workers=10
):
"""
Initialize the API using the given account info.
:param account_info: an instance of :class:`~b2sdk.v1.UrlPoolAccountInfo`,
or any custom class derived from
:class:`~b2sdk.v1.AbstractAccountInfo`
To learn more about Account Info objects, see here
:class:`~b2sdk.v1.SqliteAccountInfo`
:param cache: an instance of the one of the following classes:
:class:`~b2sdk.cache.DummyCache`, :class:`~b2sdk.cache.InMemoryCache`,
:class:`~b2sdk.cache.AuthInfoCache`,
or any custom class derived from :class:`~b2sdk.cache.AbstractCache`
It is used by B2Api to cache the mapping between bucket name and bucket ids.
default is :class:`~b2sdk.cache.DummyCache`
:param raw_api: an instance of one of the following classes:
:class:`~b2sdk.raw_api.B2RawApi`, :class:`~b2sdk.raw_simulator.RawSimulator`,
or any custom class derived from :class:`~b2sdk.raw_api.AbstractRawApi`
It makes network-less unit testing simple by using :class:`~b2sdk.raw_simulator.RawSimulator`,
in tests and :class:`~b2sdk.raw_api.B2RawApi` in production.
default is :class:`~b2sdk.raw_api.B2RawApi`
:param int max_upload_workers: a number of upload threads, default is 10
:param int max_copy_workers: a number of copy threads, default is 10
"""
self.session = self.SESSION_CLASS(account_info=account_info, cache=cache, raw_api=raw_api)
self.file_version_factory = self.FILE_VERSION_FACTORY_CLASS(self)
self.services = Services(
self,
max_upload_workers=max_upload_workers,
max_copy_workers=max_copy_workers,
)
@property
def account_info(self):
return self.session.account_info
@property
def cache(self):
return self.session.cache
@property
def raw_api(self):
"""
.. warning::
:class:`~b2sdk.raw_api.B2RawApi` attribute is deprecated.
:class:`~b2sdk.session.B2Session` expose all :class:`~b2sdk.raw_api.B2RawApi` methods now."""
return self.session.raw_api
def authorize_automatically(self):
"""
Perform automatic account authorization, retrieving all account data
from account info object passed during initialization.
"""
return self.session.authorize_automatically()
@limit_trace_arguments(only=('self', 'realm'))
def authorize_account(self, realm, application_key_id, application_key):
"""
Perform account authorization.
:param str realm: a realm to authorize account in (usually just "production")
:param str application_key_id: :term:`application key ID`
:param str application_key: user's :term:`application key`
"""
self.session.authorize_account(realm, application_key_id, application_key)
def get_account_id(self):
"""
Return the account ID.
:rtype: str
"""
return self.account_info.get_account_id()
# buckets
def create_bucket(
self,
name,
bucket_type,
bucket_info=None,
cors_rules=None,
lifecycle_rules=None,
default_server_side_encryption: Optional[EncryptionSetting] = None,
is_file_lock_enabled: Optional[bool] = None,
):
"""
Create a bucket.
:param str name: bucket name
:param str bucket_type: a bucket type, could be one of the following values: ``"allPublic"``, ``"allPrivate"``
:param dict bucket_info: additional bucket info to store with the bucket
:param dict cors_rules: bucket CORS rules to store with the bucket
:param dict lifecycle_rules: bucket lifecycle rules to store with the bucket
:param b2sdk.v1.EncryptionSetting default_server_side_encryption: default server side encryption settings (``None`` if unknown)
:param bool is_file_lock_enabled: boolean value specifies whether bucket is File Lock-enabled
:return: a Bucket object
:rtype: b2sdk.v1.Bucket
"""
account_id = self.account_info.get_account_id()
response = self.session.create_bucket(
account_id,
name,
bucket_type,
bucket_info=bucket_info,
cors_rules=cors_rules,
lifecycle_rules=lifecycle_rules,
default_server_side_encryption=default_server_side_encryption,
is_file_lock_enabled=is_file_lock_enabled,
)
bucket = self.BUCKET_FACTORY_CLASS.from_api_bucket_dict(self, response)
assert name == bucket.name, 'API created a bucket with different name\
than requested: %s != %s' % (name, bucket.name)
assert bucket_type == bucket.type_, 'API created a bucket with different type\
than requested: %s != %s' % (
bucket_type, bucket.type_
)
self.cache.save_bucket(bucket)
return bucket
def download_file_by_id(
self,
file_id,
download_dest,
progress_listener=None,
range_=None,
encryption: Optional[EncryptionSetting] = None,
):
"""
Download a file with the given ID.
:param str file_id: a file ID
:param download_dest: an instance of the one of the following classes: \
:class:`~b2sdk.v1.DownloadDestLocalFile`,\
:class:`~b2sdk.v1.DownloadDestBytes`,\
:class:`~b2sdk.v1.DownloadDestProgressWrapper`,\
:class:`~b2sdk.v1.PreSeekedDownloadDest`,\
or any sub class of :class:`~b2sdk.v1.AbstractDownloadDestination`
:param progress_listener: an instance of the one of the following classes: \
:class:`~b2sdk.v1.PartProgressReporter`,\
:class:`~b2sdk.v1.TqdmProgressListener`,\
:class:`~b2sdk.v1.SimpleProgressListener`,\
:class:`~b2sdk.v1.DoNothingProgressListener`,\
:class:`~b2sdk.v1.ProgressListenerForTest`,\
:class:`~b2sdk.v1.SyncFileReporter`,\
or any sub class of :class:`~b2sdk.v1.AbstractProgressListener`
:param list range_: a list of two integers, the first one is a start\
position, and the second one is the end position in the file
:param b2sdk.v1.EncryptionSetting encryption: encryption settings (``None`` if unknown)
:return: context manager that returns an object that supports iter_content()
"""
url = self.session.get_download_url_by_id(file_id)
return self.services.download_manager.download_file_from_url(
url,
download_dest,
progress_listener,
range_,
encryption,
)
def update_file_retention(
self,
file_id: str,
file_name: str,
file_retention: FileRetentionSetting,
bypass_governance: bool = False,
):
self.session.update_file_retention(
file_id,
file_name,
file_retention,
bypass_governance,
)
def update_file_legal_hold(
self,
file_id: str,
file_name: str,
legal_hold: LegalHold,
):
self.session.update_file_legal_hold(
file_id,
file_name,
legal_hold,
)
def get_bucket_by_id(self, bucket_id: str) -> Bucket:
"""
Return the Bucket matching the given bucket_id.
:raises b2sdk.v1.exception.NonExistentBucket: if the bucket does not exist in the account
"""
# Give a useful warning if the current application key does not
# allow access to bucket.
self.check_bucket_id_restrictions(bucket_id)
# First, try the cache.
bucket_name = self.cache.get_bucket_name_or_none_from_bucket_id(bucket_id)
if bucket_name is not None:
return self.BUCKET_CLASS(self, bucket_id, name=bucket_name)
# Second, ask the service
for bucket in self.list_buckets(bucket_id=bucket_id):
assert bucket.id_ == bucket_id
return bucket
# There is no such bucket.
raise NonExistentBucket(bucket_name)
def get_bucket_by_name(self, bucket_name):
"""
Return the Bucket matching the given bucket_name.
:param str bucket_name: the name of the bucket to return
:return: a Bucket object
:rtype: b2sdk.v1.Bucket
:raises b2sdk.v1.exception.NonExistentBucket: if the bucket does not exist in the account
"""
# Give a useful warning if the current application key does not
# allow access to the named bucket.
self.check_bucket_name_restrictions(bucket_name)
# First, try the cache.
id_ = self.cache.get_bucket_id_or_none_from_bucket_name(bucket_name)
if id_ is not None:
return self.BUCKET_CLASS(self, id_, name=bucket_name)
# Second, ask the service
for bucket in self.list_buckets(bucket_name=bucket_name):
assert bucket.name.lower() == bucket_name.lower()
return bucket
# There is no such bucket.
raise NonExistentBucket(bucket_name)
def delete_bucket(self, bucket):
"""
Delete a chosen bucket.
:param b2sdk.v1.Bucket bucket: a :term:`bucket` to delete
:rtype: None
"""
account_id = self.account_info.get_account_id()
self.session.delete_bucket(account_id, bucket.id_)
def list_buckets(self, bucket_name=None, bucket_id=None):
"""
Call ``b2_list_buckets`` and return a list of buckets.
When no bucket name nor ID is specified, returns *all* of the buckets
in the account. When a bucket name or ID is given, returns just that
bucket. When authorized with an :term:`application key` restricted to
one :term:`bucket`, you must specify the bucket name or bucket id, or
the request will be unauthorized.
:param str bucket_name: the name of the one bucket to return
:param str bucket_id: the ID of the one bucket to return
:rtype: list[b2sdk.v1.Bucket]
"""
# Give a useful warning if the current application key does not
# allow access to the named bucket.
if bucket_name is not None and bucket_id is not None:
raise ValueError('Provide either bucket_name or bucket_id, not both')
if bucket_id:
self.check_bucket_id_restrictions(bucket_id)
else:
self.check_bucket_name_restrictions(bucket_name)
account_id = self.account_info.get_account_id()
response = self.session.list_buckets(
account_id, bucket_name=bucket_name, bucket_id=bucket_id
)
buckets = self.BUCKET_FACTORY_CLASS.from_api_response(self, response)
if bucket_name or bucket_id:
# If a bucket_name or bucket_id is specified we don't clear the cache because the other buckets could still
# be valid. So we save the one bucket returned from the list_buckets call.
for bucket in buckets:
self.cache.save_bucket(bucket)
else:
# Otherwise we want to clear the cache and save the buckets returned from list_buckets
# since we just got a new list of all the buckets for this account.
self.cache.set_bucket_name_cache(buckets)
return buckets
def list_parts(self, file_id, start_part_number=None, batch_size=None):
"""
Generator that yields a :py:class:`b2sdk.v1.Part` for each of the parts that have been uploaded.
:param str file_id: the ID of the large file that is not finished
:param int start_part_number: the first part number to return; defaults to the first part
:param int batch_size: the number of parts to fetch at a time from the server
:rtype: generator
"""
return self.services.large_file.list_parts(
file_id, start_part_number=start_part_number, batch_size=batch_size
)
# delete/cancel
def cancel_large_file(self, file_id: str) -> FileIdAndName:
"""
Cancel a large file upload.
"""
return self.services.large_file.cancel_large_file(file_id)
def delete_file_version(self, file_id: str, file_name: str) -> FileIdAndName:
"""
Permanently and irrevocably delete one version of a file.
"""
# filename argument is not first, because one day it may become optional
response = self.session.delete_file_version(file_id, file_name)
return FileIdAndName.from_cancel_or_delete_response(response)
# download
def get_download_url_for_fileid(self, file_id):
"""
Return a URL to download the given file by ID.
:param str file_id: a file ID
"""
url = url_for_api(self.account_info, 'b2_download_file_by_id')
return '%s?fileId=%s' % (url, file_id)
def get_download_url_for_file_name(self, bucket_name, file_name):
"""
Return a URL to download the given file by name.
:param str bucket_name: a bucket name
:param str file_name: a file name
"""
self.check_bucket_name_restrictions(bucket_name)
return '%s/file/%s/%s' % (
self.account_info.get_download_url(), bucket_name, b2_url_encode(file_name)
)
# keys
def create_key(
self,
capabilities,
key_name,
valid_duration_seconds=None,
bucket_id=None,
name_prefix=None,
):
"""
Create a new :term:`application key`.
:param list capabilities: a list of capabilities
:param str key_name: a name of a key
:param int,None valid_duration_seconds: key auto-expire time after it is created, in seconds, or ``None`` to not expire
:param str,None bucket_id: a bucket ID to restrict the key to, or ``None`` to not restrict
:param str,None name_prefix: a remote filename prefix to restrict the key to or ``None`` to not restrict
"""
account_id = self.account_info.get_account_id()
response = self.session.create_key(
account_id,
capabilities=capabilities,
key_name=key_name,
valid_duration_seconds=valid_duration_seconds,
bucket_id=bucket_id,
name_prefix=name_prefix
)
assert set(response['capabilities']) == set(capabilities)
assert response['keyName'] == key_name
return response
def delete_key(self, application_key_id):
"""
Delete :term:`application key`.
:param str application_key_id: an :term:`application key ID`
"""
response = self.session.delete_key(application_key_id=application_key_id)
return response
def list_keys(self, start_application_key_id=None):
"""
List application keys.
:param str,None start_application_key_id: an :term:`application key ID` to start from or ``None`` to start from the beginning
"""
account_id = self.account_info.get_account_id()
return self.session.list_keys(
account_id, max_key_count=1000, start_application_key_id=start_application_key_id
)
# other
def get_file_info(self, file_id: str) -> Dict[str, Any]:
"""
Legacy interface which just returns whatever remote API returns.
.. todo::
get_file_info() should return a File with .delete(), copy(), rename(), read() and so on
:param str file_id: the id of the file who's info will be retrieved.
:return: The parsed response
:rtype: dict
"""
return self.session.get_file_info_by_id(file_id)
def check_bucket_name_restrictions(self, bucket_name: str):
"""
Check to see if the allowed field from authorize-account has a bucket restriction.
If it does, checks if the bucket_name for a given api call matches that.
If not, it raises a :py:exc:`b2sdk.v1.exception.RestrictedBucket` error.
:raises b2sdk.v1.exception.RestrictedBucket: if the account is not allowed to use this bucket
"""
self._check_bucket_restrictions('bucketName', bucket_name)
def check_bucket_id_restrictions(self, bucket_id: str):
"""
Check to see if the allowed field from authorize-account has a bucket restriction.
If it does, checks if the bucket_id for a given api call matches that.
If not, it raises a :py:exc:`b2sdk.v1.exception.RestrictedBucket` error.
:raises b2sdk.v1.exception.RestrictedBucket: if the account is not allowed to use this bucket
"""
self._check_bucket_restrictions('bucketId', bucket_id)
def _check_bucket_restrictions(self, key, value):
allowed = self.account_info.get_allowed()
allowed_bucket_identifier = allowed[key]
if allowed_bucket_identifier is not None:
if allowed_bucket_identifier != value:
raise RestrictedBucket(allowed_bucket_identifier)