|
1
|
|
|
""" |
|
2
|
|
|
Copyright George Sibble 2018 |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
1 |
|
from ..b2_exceptions import B2Exception, B2InvalidBucketName, B2InvalidBucketConfiguration |
|
6
|
1 |
|
from .bucket import B2Bucket |
|
7
|
1 |
|
from ..api import API |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
class B2Buckets(object): |
|
|
|
|
|
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
""" |
|
14
|
1 |
|
public = 'allPublic' |
|
15
|
1 |
|
private = 'allPrivate' |
|
16
|
|
|
|
|
17
|
1 |
|
def __init__(self, connector, single_bucket=None): |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
:param connector: |
|
21
|
|
|
:param str single_bucket: If any, the ID of the single bucket that we can list. |
|
22
|
|
|
""" |
|
23
|
|
|
self.connector = connector |
|
24
|
|
|
self._single_bucket = single_bucket |
|
25
|
|
|
self._buckets_by_name = {} |
|
26
|
|
|
self._buckets_by_id = {} |
|
27
|
|
|
|
|
28
|
1 |
|
def all(self): |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
:return: |
|
32
|
|
|
""" |
|
33
|
|
|
return self._update_bucket_list(retrieve=True) |
|
34
|
|
|
|
|
35
|
1 |
|
def _update_bucket_list(self, retrieve=False): |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
:param retrieve: |
|
39
|
|
|
:return: |
|
40
|
|
|
""" |
|
41
|
|
|
path = API.list_all_buckets |
|
42
|
|
|
params = {} |
|
43
|
|
|
if self._single_bucket: |
|
44
|
|
|
params['bucketId'] = self._single_bucket |
|
45
|
|
|
response = self.connector.make_request(path=path, method='post', account_id_required=True, params=params) |
|
|
|
|
|
|
46
|
|
|
if response.status_code == 200: |
|
47
|
|
|
response_json = response.json() |
|
48
|
|
|
buckets = [] |
|
49
|
|
|
self._buckets_by_name = {} |
|
50
|
|
|
self._buckets_by_id = {} |
|
51
|
|
|
for bucket_json in response_json['buckets']: |
|
52
|
|
|
new_bucket = B2Bucket(connector=self.connector, parent_list=self, **bucket_json) |
|
53
|
|
|
buckets.append(new_bucket) |
|
54
|
|
|
self._buckets_by_name[bucket_json['bucketName']] = new_bucket |
|
55
|
|
|
self._buckets_by_id[bucket_json['bucketId']] = new_bucket |
|
56
|
|
|
if retrieve: |
|
57
|
|
|
return buckets |
|
58
|
|
|
else: |
|
59
|
|
|
raise B2Exception.parse(response) |
|
60
|
|
|
|
|
61
|
1 |
|
def get(self, bucket_name=None, bucket_id=None): |
|
62
|
|
|
""" |
|
63
|
|
|
|
|
64
|
|
|
:param bucket_name: |
|
65
|
|
|
:param bucket_id: |
|
66
|
|
|
:return: |
|
67
|
|
|
""" |
|
68
|
|
|
self._update_bucket_list() |
|
69
|
|
|
if bucket_name is not None: |
|
|
|
|
|
|
70
|
|
|
return self._buckets_by_name.get(bucket_name, None) |
|
71
|
|
|
else: |
|
72
|
|
|
return self._buckets_by_id.get(bucket_id, None) |
|
73
|
|
|
|
|
74
|
1 |
|
def create(self, bucket_name, security, configuration=None): |
|
75
|
|
|
""" |
|
76
|
|
|
|
|
77
|
|
|
:param bucket_name: |
|
78
|
|
|
:param configuration: |
|
79
|
|
|
:return: |
|
80
|
|
|
""" |
|
81
|
|
|
path = API.create_bucket |
|
82
|
|
|
if type(bucket_name) != str and type(bucket_name) != bytes: |
|
|
|
|
|
|
83
|
|
|
raise B2InvalidBucketName("Bucket name must be alphanumeric or '-") |
|
84
|
|
|
if type(configuration) != dict and configuration is not None: |
|
|
|
|
|
|
85
|
|
|
raise B2InvalidBucketConfiguration |
|
86
|
|
|
params = { |
|
87
|
|
|
'bucketName': bucket_name, |
|
88
|
|
|
'bucketType': security, |
|
89
|
|
|
#TODO: bucketInfo |
|
|
|
|
|
|
90
|
|
|
#TODO: corsRules |
|
|
|
|
|
|
91
|
|
|
#TODO: lifeCycleRules |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
response = self.connector.make_request(path=path, method='post', params=params, account_id_required=True) |
|
|
|
|
|
|
94
|
|
|
if response.status_code == 200: |
|
95
|
|
|
bucket_json = response.json() |
|
96
|
|
|
new_bucket = B2Bucket(connector=self.connector, parent_list=self, **bucket_json) |
|
97
|
|
|
self._buckets_by_name[bucket_json['bucketName']] = new_bucket |
|
98
|
|
|
self._buckets_by_id[bucket_json['bucketId']] = new_bucket |
|
99
|
|
|
return new_bucket |
|
100
|
|
|
else: |
|
101
|
|
|
raise B2Exception.parse(response) |
|
|
|
|
|