Exemple #1
0
def download_file(source_bucket, source_path, dest_path,
                  create_subdirs_if_needed=False):
  """ Downloads a single file from Google Cloud Storage to local disk.

  Args:
    source_bucket: GCS bucket to download the file from
    source_path: full path (Posix-style) within that bucket
    dest_path: full path (local-OS-style) on local disk to copy the file to
    create_subdirs_if_needed: boolean; whether to create subdirectories as
        needed to create dest_path
  """
  source_http_url = posixpath.join(
      'http://storage.googleapis.com', source_bucket, source_path)
  url_utils.copy_contents(source_url=source_http_url, dest_path=dest_path,
                          create_subdirs_if_needed=create_subdirs_if_needed)
Exemple #2
0
def download_file(source_bucket, source_path, dest_path,
                  create_subdirs_if_needed=False):
  """ Downloads a single file from Google Cloud Storage to local disk.

  Args:
    source_bucket: GCS bucket to download the file from
    source_path: full path (Posix-style) within that bucket
    dest_path: full path (local-OS-style) on local disk to copy the file to
    create_subdirs_if_needed: boolean; whether to create subdirectories as
        needed to create dest_path
  """
  source_http_url = posixpath.join(
      'http://storage.googleapis.com', source_bucket, source_path)
  url_utils.copy_contents(source_url=source_http_url, dest_path=dest_path,
                          create_subdirs_if_needed=create_subdirs_if_needed)
 def test_copy_contents(self):
   """Tests copy_contents(). """
   contents = 'these are the contents'
   tempdir_path = tempfile.mkdtemp()
   try:
     source_path = os.path.join(tempdir_path, 'source')
     source_url = url_utils.create_filepath_url(source_path)
     with open(source_path, 'w') as source_handle:
       source_handle.write(contents)
     dest_path = os.path.join(tempdir_path, 'new_subdir', 'dest')
     # Destination subdir does not exist, so copy_contents() should fail
     # if create_subdirs_if_needed is False.
     with self.assertRaises(Exception):
       url_utils.copy_contents(source_url=source_url,
                               dest_path=dest_path,
                               create_subdirs_if_needed=False)
     # If create_subdirs_if_needed is True, it should work.
     url_utils.copy_contents(source_url=source_url,
                             dest_path=dest_path,
                             create_subdirs_if_needed=True)
     self.assertEquals(open(dest_path).read(), contents)
   finally:
     shutil.rmtree(tempdir_path)
Exemple #4
0
 def test_copy_contents(self):
     """Tests copy_contents(). """
     contents = 'these are the contents'
     tempdir_path = tempfile.mkdtemp()
     try:
         source_path = os.path.join(tempdir_path, 'source')
         source_url = url_utils.create_filepath_url(source_path)
         with open(source_path, 'w') as source_handle:
             source_handle.write(contents)
         dest_path = os.path.join(tempdir_path, 'new_subdir', 'dest')
         # Destination subdir does not exist, so copy_contents() should fail
         # if create_subdirs_if_needed is False.
         with self.assertRaises(Exception):
             url_utils.copy_contents(source_url=source_url,
                                     dest_path=dest_path,
                                     create_subdirs_if_needed=False)
         # If create_subdirs_if_needed is True, it should work.
         url_utils.copy_contents(source_url=source_url,
                                 dest_path=dest_path,
                                 create_subdirs_if_needed=True)
         self.assertEquals(open(dest_path).read(), contents)
     finally:
         shutil.rmtree(tempdir_path)