-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathbucket.py
More file actions
526 lines (406 loc) · 18.4 KB
/
bucket.py
File metadata and controls
526 lines (406 loc) · 18.4 KB
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
"""Create / interact with gcloud storage buckets."""
import os
from gcloud.storage._helpers import _MetadataMixin
from gcloud.storage import exceptions
from gcloud.storage.acl import BucketACL
from gcloud.storage.acl import DefaultObjectACL
from gcloud.storage.iterator import Iterator
from gcloud.storage.key import Key
from gcloud.storage.key import _KeyIterator
class Bucket(_MetadataMixin):
"""A class representing a Bucket on Cloud Storage.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: The connection to use when sending requests.
:type name: string
:param name: The name of the bucket.
"""
CUSTOM_METADATA_FIELDS = {
'acl': 'get_acl',
'defaultObjectAcl': 'get_default_object_acl',
'lifecycle': 'get_lifecycle',
'versioning': 'get_versioning',
}
"""Mapping of field name -> accessor for fields w/ custom accessors."""
# ACL rules are lazily retrieved.
_acl = _default_object_acl = None
def __init__(self, connection=None, name=None, metadata=None):
super(Bucket, self).__init__(name=name, metadata=metadata)
self._connection = connection
@property
def acl(self):
"""Create our ACL on demand."""
if self._acl is None:
self._acl = BucketACL(self)
return self._acl
@property
def default_object_acl(self):
"""Create our defaultObjectACL on demand."""
if self._default_object_acl is None:
self._default_object_acl = DefaultObjectACL(self)
return self._default_object_acl
@classmethod
def from_dict(cls, bucket_dict, connection=None):
"""Construct a new bucket from a dictionary of data from Cloud Storage.
:type bucket_dict: dict
:param bucket_dict: The dictionary of data to construct a bucket from.
:rtype: :class:`Bucket`
:returns: A bucket constructed from the data provided.
"""
return cls(connection=connection, name=bucket_dict['name'],
metadata=bucket_dict)
def __repr__(self):
return '<Bucket: %s>' % self.name
def __iter__(self):
return iter(_KeyIterator(bucket=self))
def __contains__(self, key):
return self.get_key(key) is not None
@property
def connection(self):
"""Getter property for the connection to use with this Bucket.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: The connection to use.
"""
return self._connection
@property
def path(self):
"""The URL path to this bucket."""
if not self.name:
raise ValueError('Cannot determine path without bucket name.')
return '/b/' + self.name
def get_key(self, key):
"""Get a key object by name.
This will return None if the key doesn't exist::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> print bucket.get_key('/path/to/key.txt')
<Key: my-bucket, /path/to/key.txt>
>>> print bucket.get_key('/does-not-exist.txt')
None
:type key: string or :class:`gcloud.storage.key.Key`
:param key: The name of the key to retrieve.
:rtype: :class:`gcloud.storage.key.Key` or None
:returns: The key object if it exists, otherwise None.
"""
# Coerce this to a key object (either from a Key or a string).
key = self.new_key(key)
try:
response = self.connection.api_request(method='GET', path=key.path)
return Key.from_dict(response, bucket=self)
except exceptions.NotFoundError:
return None
def get_all_keys(self):
"""List all the keys in this bucket.
This will **not** retrieve all the data for all the keys, it
will only retrieve metadata about the keys.
This is equivalent to::
keys = [key for key in bucket]
:rtype: list of :class:`gcloud.storage.key.Key`
:returns: A list of all the Key objects in this bucket.
"""
return list(self)
def new_key(self, key):
"""Given path name (or Key), return a :class:`.storage.key.Key` object.
This is really useful when you're not sure if you have a Key
object or a string path name. Given either of those types, this
returns the corresponding Key object.
:type key: string or :class:`gcloud.storage.key.Key`
:param key: A path name or actual key object.
:rtype: :class:`gcloud.storage.key.Key`
:returns: A Key object with the path provided.
"""
if isinstance(key, Key):
return key
# Support Python 2 and 3.
try:
string_type = basestring
except NameError: # pragma: NO COVER PY3k
string_type = str
if isinstance(key, string_type):
return Key(bucket=self, name=key)
raise TypeError('Invalid key: %s' % key)
def delete(self, force=False):
"""Delete this bucket.
The bucket **must** be empty in order to delete it. If the
bucket doesn't exist, this will raise a
:class:`gcloud.storage.exceptions.NotFoundError`. If the bucket
is not empty, this will raise an Exception.
If you want to delete a non-empty bucket you can pass in a force
parameter set to true. This will iterate through the bucket's
keys and delete the related objects, before deleting the bucket.
:type force: bool
:param full: If True, empties the bucket's objects then deletes it.
:raises: :class:`gcloud.storage.exceptions.NotFoundError` if the
bucket does not exist, or
:class:`gcloud.storage.exceptions.ConnectionError` if the
bucket has keys and `force` is not passed.
"""
return self.connection.delete_bucket(self.name, force=force)
def delete_key(self, key):
"""Deletes a key from the current bucket.
If the key isn't found,
this will throw a :class:`gcloud.storage.exceptions.NotFoundError`.
For example::
>>> from gcloud import storage
>>> from gcloud.storage import exceptions
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> print bucket.get_all_keys()
[<Key: my-bucket, my-file.txt>]
>>> bucket.delete_key('my-file.txt')
>>> try:
... bucket.delete_key('doesnt-exist')
... except exceptions.NotFoundError:
... pass
:type key: string or :class:`gcloud.storage.key.Key`
:param key: A key name or Key object to delete.
:rtype: :class:`gcloud.storage.key.Key`
:returns: The key that was just deleted.
:raises: :class:`gcloud.storage.exceptions.NotFoundError` (to suppress
the exception, call ``delete_keys``, passing a no-op
``on_error`` callback, e.g.::
>>> bucket.delete_keys([key], on_error=lambda key: pass)
"""
key = self.new_key(key)
self.connection.api_request(method='DELETE', path=key.path)
return key
def delete_keys(self, keys, on_error=None):
"""Deletes a list of keys from the current bucket.
Uses :func:`Bucket.delete_key` to delete each individual key.
:type keys: list of string or :class:`gcloud.storage.key.Key`
:param keys: A list of key names or Key objects to delete.
:type on_error: a callable taking (key)
:param on_error: If not ``None``, called once for each key raising
:class:`gcloud.storage.exceptions.NotFoundError`;
otherwise, the exception is propagated.
:raises: :class:`gcloud.storage.exceptions.NotFoundError` (if
`on_error` is not passed).
"""
for key in keys:
try:
self.delete_key(key)
except exceptions.NotFoundError:
if on_error is not None:
on_error(key)
else:
raise
def copy_key(self, key, destination_bucket, new_name=None):
"""Copy the given key to the given bucket, optionally with a new name.
:type key: string or :class:`gcloud.storage.key.Key`
:param key: The key to be copied.
:type destination_bucket: :class:`gcloud.storage.bucket.Bucket`
:param destination_bucket: The bucket into which the key should be
copied.
:type new_name: string
:param new_name: (optional) the new name for the copied file.
:rtype: :class:`gcloud.storage.key.Key`
:returns: The new Key.
"""
if new_name is None:
new_name = key.name
new_key = destination_bucket.new_key(new_name)
api_path = key.path + '/copyTo' + new_key.path
self.connection.api_request(method='POST', path=api_path)
return new_key
def upload_file(self, filename, key=None):
"""Shortcut method to upload a file into this bucket.
Use this method to quickly put a local file in Cloud Storage.
For example::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
>>> print bucket.get_all_keys()
[<Key: my-bucket, remote-text-file.txt>]
If you don't provide a key value, we will try to upload the file
using the local filename as the key (**not** the complete
path)::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file('~/my-file.txt')
>>> print bucket.get_all_keys()
[<Key: my-bucket, my-file.txt>]
:type filename: string
:param filename: Local path to the file you want to upload.
:type key: string or :class:`gcloud.storage.key.Key`
:param key: The key (either an object or a remote path) of where
to put the file. If this is blank, we will try to
upload the file to the root of the bucket with the
same name as on your local file system.
"""
if key is None:
key = os.path.basename(filename)
key = self.new_key(key)
return key.upload_from_filename(filename)
def upload_file_object(self, file_obj, key=None):
"""Shortcut method to upload a file object into this bucket.
Use this method to quickly put a local file in Cloud Storage.
For example::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
>>> print bucket.get_all_keys()
[<Key: my-bucket, remote-text-file.txt>]
If you don't provide a key value, we will try to upload the file
using the local filename as the key (**not** the complete
path)::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file(open('~/my-file.txt'))
>>> print bucket.get_all_keys()
[<Key: my-bucket, my-file.txt>]
:type file_obj: file
:param file_obj: A file handle open for reading.
:type key: string or :class:`gcloud.storage.key.Key`
:param key: The key (either an object or a remote path) of where
to put the file. If this is blank, we will try to
upload the file to the root of the bucket with the
same name as on your local file system.
"""
if key:
key = self.new_key(key)
else:
key = self.new_key(os.path.basename(file_obj.name))
return key.upload_from_file(file_obj)
def configure_website(self, main_page_suffix=None, not_found_page=None):
"""Configure website-related metadata.
.. note::
This (apparently) only works
if your bucket name is a domain name
(and to do that, you need to get approved somehow...).
Check out the official documentation here:
https://developers.google.com/storage/docs/website-configuration
If you want this bucket to host a website, just provide the name
of an index page and a page to use when a key isn't found::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email,
private_key_path)
>>> bucket = connection.get_bucket(bucket_name)
>>> bucket.configure_website('index.html', '404.html')
You probably should also make the whole bucket public::
>>> bucket.make_public(recursive=True, future=True)
This says: "Make the bucket public, and all the stuff already in
the bucket, and anything else I add to the bucket. Just make it
all public."
:type main_page_suffix: string
:param main_page_suffix: The page to use as the main page
of a directory.
Typically something like index.html.
:type not_found_page: string
:param not_found_page: The file to use when a page isn't found.
"""
data = {
'website': {
'mainPageSuffix': main_page_suffix,
'notFoundPage': not_found_page,
},
}
return self.patch_metadata(data)
def disable_website(self):
"""Disable the website configuration for this bucket.
This is really just a shortcut for setting the website-related
attributes to ``None``.
"""
return self.configure_website(None, None)
def get_acl(self):
"""Get ACL metadata as a :class:`gcloud.storage.acl.BucketACL` object.
:rtype: :class:`gcloud.storage.acl.BucketACL`
:returns: An ACL object for the current bucket.
"""
if not self.acl.loaded:
self.acl.reload()
return self.acl
def get_default_object_acl(self):
"""Get the current Default Object ACL rules.
If the appropriate metadata isn't available locally, this method
will reload it from Cloud Storage.
:rtype: :class:`gcloud.storage.acl.DefaultObjectACL`
:returns: A DefaultObjectACL object for this bucket.
"""
if not self.default_object_acl.loaded:
self.default_object_acl.reload()
return self.default_object_acl
def make_public(self, recursive=False, future=False):
"""Make a bucket public.
:type recursive: bool
:param recursive: If True, this will make all keys inside the bucket
public as well.
:type future: bool
:param future: If True, this will make all objects created in the
future public as well.
"""
self.get_acl().all().grant_read()
self.acl.save()
if future:
doa = self.get_default_object_acl()
doa.all().grant_read()
doa.save()
if recursive:
for key in self:
key.get_acl().all().grant_read()
key.save_acl()
def get_lifecycle(self):
"""Retrieve CORS policies configured for this bucket.
See: https://cloud.google.com/storage/docs/lifecycle and
https://cloud.google.com/storage/docs/json_api/v1/buckets
:rtype: list(dict)
:returns: A sequence of mappings describing each CORS policy.
"""
if not self.has_metadata('lifecycle'):
self.reload_metadata()
result = []
info = self.metadata.get('lifecycle', {})
for rule in info.get('rule', ()):
rule = rule.copy()
result.append(rule)
return result
def update_lifecycle(self, rules):
"""Update CORS policies configured for this bucket.
See: https://cloud.google.com/storage/docs/lifecycle and
https://cloud.google.com/storage/docs/json_api/v1/buckets
:type rules: list(dict)
:param rules: A sequence of mappings describing each lifecycle policy.
"""
self.patch_metadata({'lifecycle': {'rule': rules}})
def get_versioning(self):
"""Is versioning enabled for this bucket?
See: https://cloud.google.com/storage/docs/object-versioning for
details.
:rtype: boolean
:returns: True if enabled, else False.
"""
if not self.has_metadata(field='versioning'):
self.reload_metadata()
versioning = self.metadata.get('versioning', {})
return versioning.get('enabled', False)
def enable_versioning(self):
"""Enable versioning for this bucket.
See: https://cloud.google.com/storage/docs/object-versioning for
details.
"""
self.patch_metadata({'versioning': {'enabled': True}})
def disable_versioning(self):
"""Disable versioning for this bucket.
See: https://cloud.google.com/storage/docs/object-versioning for
details.
"""
self.patch_metadata({'versioning': {'enabled': False}})
class BucketIterator(Iterator):
"""An iterator listing all buckets.
You shouldn't have to use this directly, but instead should use the helper
methods on :class:`gcloud.storage.connection.Connection` objects.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: The connection to use for querying the list of buckets.
"""
def __init__(self, connection):
super(BucketIterator, self).__init__(connection=connection, path='/b')
def get_items_from_response(self, response):
"""Factory method which yields :class:`.Bucket` items from a response.
:type response: dict
:param response: The JSON API response for a page of buckets.
"""
for item in response.get('items', []):
yield Bucket.from_dict(item, connection=self.connection)