OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # pylint: disable=C0301 | 3 # pylint: disable=C0301 |
4 """ | 4 """ |
5 Copyright 2014 Google Inc. | 5 Copyright 2014 Google Inc. |
6 | 6 |
7 Use of this source code is governed by a BSD-style license that can be | 7 Use of this source code is governed by a BSD-style license that can be |
8 found in the LICENSE file. | 8 found in the LICENSE file. |
9 | 9 |
10 Utilities for accessing Google Cloud Storage, using the boto library (wrapper | 10 Utilities for accessing Google Cloud Storage, using the boto library (wrapper |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 q.task_done() | 404 q.task_done() |
405 for _ in range(num_threads): | 405 for _ in range(num_threads): |
406 t = threading.Thread(target=worker) | 406 t = threading.Thread(target=worker) |
407 t.daemon = True | 407 t.daemon = True |
408 t.start() | 408 t.start() |
409 | 409 |
410 # Block until all files have been uploaded and all workers have exited. | 410 # Block until all files have been uploaded and all workers have exited. |
411 q.join() | 411 q.join() |
412 | 412 |
413 def download_file(self, source_bucket, source_path, dest_path, | 413 def download_file(self, source_bucket, source_path, dest_path, |
414 create_subdirs_if_needed=False): | 414 create_subdirs_if_needed=False, source_generation=None): |
415 """Downloads a single file from Google Cloud Storage to local disk. | 415 """Downloads a single file from Google Cloud Storage to local disk. |
416 | 416 |
417 Args: | 417 Args: |
418 source_bucket: GS bucket to download the file from | 418 source_bucket: GS bucket to download the file from |
419 source_path: full path (Posix-style) within that bucket | 419 source_path: full path (Posix-style) within that bucket |
420 dest_path: full path (local-OS-style) on local disk to copy the file to | 420 dest_path: full path (local-OS-style) on local disk to copy the file to |
421 create_subdirs_if_needed: boolean; whether to create subdirectories as | 421 create_subdirs_if_needed: boolean; whether to create subdirectories as |
422 needed to create dest_path | 422 needed to create dest_path |
423 source_generation: the generation version of the source. | |
rmistry
2014/11/04 12:42:53
Nitiest of nits: Please remove the period (to be c
bungeman-skia
2014/11/04 14:36:47
Done.
| |
423 """ | 424 """ |
424 b = self._connect_to_bucket(bucket=source_bucket) | 425 b = self._connect_to_bucket(bucket=source_bucket) |
425 key = Key(b) | 426 key = Key(b) |
426 key.name = source_path | 427 key.name = source_path |
428 if (source_generation): | |
rmistry
2014/11/04 12:42:52
Surrounding parenthesis not needed ->
if source_g
bungeman-skia
2014/11/04 14:36:47
Done.
| |
429 key.generation = source_generation | |
427 if create_subdirs_if_needed: | 430 if create_subdirs_if_needed: |
428 _makedirs_if_needed(os.path.dirname(dest_path)) | 431 _makedirs_if_needed(os.path.dirname(dest_path)) |
429 with open(dest_path, 'w') as f: | 432 with open(dest_path, 'w') as f: |
430 try: | 433 try: |
431 key.get_contents_to_file(fp=f) | 434 key.get_contents_to_file(fp=f) |
432 except BotoServerError, e: | 435 except BotoServerError, e: |
433 e.body = (repr(e.body) + | 436 e.body = (repr(e.body) + |
434 ' while downloading gs://%s/%s to local_path=%s' % ( | 437 ' while downloading gs://%s/%s to local_path=%s' % ( |
435 b.name, source_path, dest_path)) | 438 b.name, source_path, dest_path)) |
436 raise | 439 raise |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 | 704 |
702 def _get_local_md5(path): | 705 def _get_local_md5(path): |
703 """Returns the MD5 hash of a file on local disk.""" | 706 """Returns the MD5 hash of a file on local disk.""" |
704 hasher = hashlib.md5() | 707 hasher = hashlib.md5() |
705 with open(path, 'rb') as f: | 708 with open(path, 'rb') as f: |
706 while True: | 709 while True: |
707 data = f.read(64*1024) | 710 data = f.read(64*1024) |
708 if not data: | 711 if not data: |
709 return hasher.hexdigest() | 712 return hasher.hexdigest() |
710 hasher.update(data) | 713 hasher.update(data) |
OLD | NEW |