-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathkey.py
More file actions
558 lines (424 loc) · 18 KB
/
key.py
File metadata and controls
558 lines (424 loc) · 18 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
"""Create / interact with gcloud storage keys."""
import mimetypes
import os
from StringIO import StringIO
from gcloud.storage.acl import ObjectACL
from gcloud.storage.exceptions import StorageError
from gcloud.storage.iterator import Iterator
class Key(object):
"""A wrapper around Cloud Storage's concept of an ``Object``."""
CHUNK_SIZE = 1024 * 1024 # 1 MB.
"""The size of a chunk of data whenever iterating (1 MB).
This must be a multiple of 256 KB per the API specification.
"""
def __init__(self, bucket=None, name=None, metadata=None):
"""Key constructor.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket to which this key belongs.
:type name: string
:param name: The name of the key. This corresponds to the
unique path of the object in the bucket.
:type metadata: dict
:param metadata: All the other data provided by Cloud Storage.
"""
self.bucket = bucket
self.name = name
self.metadata = metadata or {}
# Lazily get the ACL information.
self.acl = None
@classmethod
def from_dict(cls, key_dict, bucket=None):
"""Instantiate a :class:`Key` from data returned by the JSON API.
:type key_dict: dict
:param key_dict: A dictionary of data returned from getting an
Cloud Storage object.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket to which this key belongs (and by
proxy, which connection to use).
:rtype: :class:`Key`
:returns: A key based on the data provided.
"""
return cls(bucket=bucket, name=key_dict['name'], metadata=key_dict)
def __repr__(self):
if self.bucket:
bucket_name = self.bucket.name
else:
bucket_name = None
return '<Key: %s, %s>' % (bucket_name, self.name)
@property
def connection(self):
"""Getter property for the connection to use with this Key.
:rtype: :class:`gcloud.storage.connection.Connection` or None
:returns: The connection to use, or None if no connection is set.
"""
if self.bucket and self.bucket.connection:
return self.bucket.connection
@property
def path(self):
"""Getter property for the URL path to this Key.
:rtype: string
:returns: The URL path to this Key.
"""
if not self.bucket:
raise ValueError('Cannot determine path without a bucket defined.')
elif not self.name:
raise ValueError('Cannot determine path without a key name.')
return self.bucket.path + '/o/' + self.name
@property
def public_url(self):
"""The public URL for this key's object.
:rtype: `string`
:returns: The public URL for this key.
"""
return '{storage_base_url}/{self.bucket.name}/{self.name}'.format(
storage_base_url='http://commondatastorage.googleapis.com',
self=self)
def generate_signed_url(self, expiration, method='GET'):
"""Generates a signed URL for this key.
If you have a key that you want to allow access to for a set
amount of time, you can use this method to generate a URL that
is only valid within a certain time period.
This is particularly useful if you don't want publicly
accessible keys, but don't want to require users to explicitly
log in.
:type expiration: int, long, datetime.datetime, datetime.timedelta
:param expiration: When the signed URL should expire.
:type method: string
:param method: The HTTP verb that will be used when requesting the URL.
:rtype: string
:returns: A signed URL you can use to access the resource
until expiration.
"""
resource = '/{self.bucket.name}/{self.name}'.format(self=self)
return self.connection.generate_signed_url(resource=resource,
expiration=expiration,
method=method)
def exists(self):
"""Determines whether or not this key exists.
:rtype: bool
:returns: True if the key exists in Cloud Storage.
"""
return self.bucket.get_key(self.name) is not None
def rename(self, new_name):
"""Renames this key using copy and delete operations.
Effectively, copies key to the same bucket with a new name, then
deletes the key.
.. warning::
This method will first duplicate the data and then delete the
old key. This means that with very large objects renaming
could be a very (temporarily) costly or a very slow operation.
:type new_name: string
:param new_name: The new name for this key.
:rtype: :class:`Key`
:returns: The newly-copied key.
"""
new_key = self.bucket.copy_key(self, self.bucket, new_name)
self.bucket.delete_key(self)
return new_key
def delete(self):
"""Deletes a key from Cloud Storage.
:rtype: :class:`Key`
:returns: The key that was just deleted.
"""
return self.bucket.delete_key(self)
def download_to_file(self, file_obj):
"""Download the contents of this key into a file-like object.
:type file_obj: file
:param file_obj: A file handle to which to write the key's data.
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
"""
for chunk in _KeyDataIterator(self):
file_obj.write(chunk)
# NOTE: Alias for boto-like API.
get_contents_to_file = download_to_file
def download_to_filename(self, filename):
"""Download the contents of this key into a named file.
:type filename: string
:param filename: A filename to be passed to ``open``.
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
"""
with open(filename, 'wb') as file_obj:
self.download_to_file(file_obj)
# NOTE: Alias for boto-like API.
get_contents_to_filename = download_to_filename
def download_as_string(self):
"""Download the contents of this key as a string.
:rtype: string
:returns: The data stored in this key.
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
"""
string_buffer = StringIO()
self.download_to_file(string_buffer)
return string_buffer.getvalue()
# NOTE: Alias for boto-like API.
get_contents_as_string = download_as_string
def upload_from_file(self, file_obj, rewind=False, size=None,
content_type=None):
"""Upload the contents of this key from a file-like object.
:type file_obj: file
:param file_obj: A file handle open for reading.
:type rewind: bool
:param rewind: If True, seek to the beginning of the file handle before
writing the file to Cloud Storage.
:type size: int
:param size: The number of bytes to read from the file handle.
If not provided, we'll try to guess the size using
:func:`os.fstat`
"""
# Rewind the file if desired.
if rewind:
file_obj.seek(0, os.SEEK_SET)
# Get the basic stats about the file.
total_bytes = size or os.fstat(file_obj.fileno()).st_size
bytes_uploaded = 0
# Set up a resumable upload session.
headers = {
'X-Upload-Content-Type': content_type or 'application/unknown',
'X-Upload-Content-Length': total_bytes,
}
upload_url = self.connection.build_api_url(
path=self.bucket.path + '/o',
query_params={'uploadType': 'resumable', 'name': self.name},
api_base_url=self.connection.API_BASE_URL + '/upload')
response, _ = self.connection.make_request(
method='POST', url=upload_url,
headers=headers)
# Get the resumable upload URL.
upload_url = response['location']
while bytes_uploaded < total_bytes:
# Construct the range header.
data = file_obj.read(self.CHUNK_SIZE)
chunk_size = len(data)
start = bytes_uploaded
end = bytes_uploaded + chunk_size - 1
headers = {
'Content-Range': 'bytes %d-%d/%d' % (start, end, total_bytes),
}
response, _ = self.connection.make_request(
content_type='text/plain',
method='POST', url=upload_url, headers=headers, data=data)
bytes_uploaded += chunk_size
# NOTE: Alias for boto-like API.
set_contents_from_file = upload_from_file
def upload_from_filename(self, filename):
"""Upload this key's contents from the content of f named file.
:type filename: string
:param filename: The path to the file.
"""
content_type, _ = mimetypes.guess_type(filename)
with open(filename, 'rb') as file_obj:
self.upload_from_file(file_obj, content_type=content_type)
# NOTE: Alias for boto-like API.
set_contents_from_filename = upload_from_filename
def upload_from_string(self, data, content_type='text/plain'):
"""Upload contents of this key from the provided string.
:type data: string
:param data: The data to store in this key.
:rtype: :class:`Key`
:returns: The updated Key object.
"""
string_buffer = StringIO()
string_buffer.write(data)
self.set_contents_from_file(file_obj=string_buffer, rewind=True,
size=string_buffer.len,
content_type=content_type)
return self
# NOTE: Alias for boto-like API.
set_contents_from_string = upload_from_string
def has_metadata(self, field=None):
"""Check if metadata is available locally.
:type field: string
:param field: (optional) the particular field to check for.
:rtype: bool
:returns: Whether metadata is available locally.
"""
if not self.metadata:
return False
elif field and field not in self.metadata:
return False
else:
return True
def reload_metadata(self, full=False):
"""Reload metadata from Cloud Storage.
:type full: bool
:param full: If True, loads all data (include ACL data).
:rtype: :class:`Key`
:returns: The key you just reloaded data for.
"""
projection = 'full' if full else 'noAcl'
query_params = {'projection': projection}
self.metadata = self.connection.api_request(
method='GET', path=self.path, query_params=query_params)
return self
def get_metadata(self, field=None, default=None):
"""Get all metadata or a specific field.
If you request a field that isn't available, and that field can
be retrieved by refreshing data from Cloud Storage, this method
will reload the data using :func:`Key.reload_metadata`.
:type field: string
:param field: (optional) A particular field to retrieve from metadata.
:type default: anything
:param default: The value to return if the field provided wasn't found.
:rtype: dict or anything
:returns: All metadata or the value of the specific field.
"""
if not self.has_metadata(field=field):
full = (field and field == 'acl')
self.reload_metadata(full=full)
if field:
return self.metadata.get(field, default)
else:
return self.metadata
def patch_metadata(self, metadata):
"""Update particular fields of this key's metadata.
This method will only update the fields provided and will not
touch the other fields.
It will also reload the metadata locally based on the servers
response.
:type metadata: dict
:param metadata: The dictionary of values to update.
:rtype: :class:`Key`
:returns: The current key.
"""
self.metadata = self.connection.api_request(
method='PATCH', path=self.path, data=metadata,
query_params={'projection': 'full'})
return self
def reload_acl(self):
"""Reload the ACL data from Cloud Storage.
:rtype: :class:`Key`
:returns: The current key.
"""
self.acl = ObjectACL(key=self)
for entry in self.get_metadata('acl', []):
entity = self.acl.entity_from_dict(entry)
self.acl.add_entity(entity)
return self
def get_acl(self):
"""Get ACL metadata as a :class:`gcloud.storage.acl.ObjectACL` object.
:rtype: :class:`gcloud.storage.acl.ObjectACL`
:returns: An ACL object for the current key.
"""
if not self.acl:
self.reload_acl()
return self.acl
def save_acl(self, acl=None):
"""Save the ACL data for this key.
:type acl: :class:`gcloud.storage.acl.ACL`
:param acl: The ACL object to save. If left blank, this will
save the ACL set locally on the key.
"""
# We do things in this weird way because [] and None
# both evaluate to False, but mean very different things.
if acl is None:
acl = self.acl
if acl is None:
return self
self.patch_metadata({'acl': list(acl)})
self.reload_acl()
return self
def clear_acl(self):
"""Remove all ACL rules from the key.
Note that this won't actually remove *ALL* the rules, but it
will remove all the non-default rules. In short, you'll still
have access to a key that you created even after you clear ACL
rules with this method.
"""
return self.save_acl(acl=[])
def make_public(self):
"""Make this key public giving all users read access.
:rtype: :class:`Key`
:returns: The current key.
"""
self.get_acl().all().grant_read()
self.save_acl()
return self
class _KeyIterator(Iterator):
"""An iterator listing keys.
You shouldn't have to use this directly, but instead should use the
helper methods on :class:`gcloud.storage.key.Key` objects.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket from which to list keys.
"""
def __init__(self, bucket):
self.bucket = bucket
super(_KeyIterator, self).__init__(
connection=bucket.connection, path=bucket.path + '/o')
def get_items_from_response(self, response):
"""Factory method, yields :class:`.storage.key.Key` items from response.
:type response: dict
:param response: The JSON API response for a page of keys.
"""
for item in response.get('items', []):
yield Key.from_dict(item, bucket=self.bucket)
class _KeyDataIterator(object):
"""An iterator listing data stored in a key.
You shouldn't have to use this directly, but instead should use the
helper methods on :class:`gcloud.storage.key.Key` objects.
:type key: :class:`gcloud.storage.key.Key`
:param key: The key from which to list data..
"""
def __init__(self, key):
self.key = key
# NOTE: These variables will be initialized by reset().
self._bytes_written = None
self._total_bytes = None
self.reset()
def __iter__(self):
while self.has_more_data():
yield self.get_next_chunk()
def reset(self):
"""Resets the iterator to the beginning."""
self._bytes_written = 0
self._total_bytes = None
def has_more_data(self):
"""Determines whether or not this iterator has more data to read.
:rtype: bool
:returns: Whether the iterator has more data or not.
"""
if self._bytes_written == 0:
return True
elif not self._total_bytes:
# self._total_bytes **should** be set by this point.
# If it isn't, something is wrong.
raise ValueError('Size of object is unknown.')
else:
return self._bytes_written < self._total_bytes
def get_headers(self):
"""Gets range header(s) for next chunk of data.
:rtype: dict
:returns: A dictionary of query parameters.
"""
start = self._bytes_written
end = self._bytes_written + self.key.CHUNK_SIZE - 1
if self._total_bytes and end > self._total_bytes:
end = ''
return {'Range': 'bytes=%s-%s' % (start, end)}
def get_url(self):
"""Gets URL to read next chunk of data.
:rtype: string
:returns: A URL.
"""
return self.key.connection.build_api_url(
path=self.key.path, query_params={'alt': 'media'})
def get_next_chunk(self):
"""Gets the next chunk of data.
Uses CHUNK_SIZE to determine how much data to get.
:rtype: string
:returns: The chunk of data read from the key.
:raises: :class:`RuntimeError` if no more data or
:class:`gcloud.storage.exceptions.StorageError` in the
case of an unexpected response status code.
"""
if not self.has_more_data():
raise RuntimeError('No more data in this iterator. Try resetting.')
response, content = self.key.connection.make_request(
method='GET', url=self.get_url(), headers=self.get_headers())
if response.status in (200, 206):
self._bytes_written += len(content)
if 'content-range' in response:
content_range = response['content-range']
self._total_bytes = int(content_range.rsplit('/', 1)[1])
return content
# Expected a 200 or a 206. Got something else, which is unknown.
raise StorageError(response)