Exemplo n.º 1
0
def upload_to_bucket(full_object_name, local_path):
    """ Uploads a file to GCS.

  Args:
    full_object_name: A str, a full GCS object name.
    local_path: A str, the path to a local backup file.

  Returns:
    True on success, False otherwise.
  """
    # Ensure local file is accessible.
    if not backup_recovery_helper.does_file_exist(local_path):
        logging.error("Local file '{0}' doesn't exist. Aborting upload to "
                      "GCS.".format(local_path))
        return False

    # Extract bucket and object name for GCS.
    bucket_name, object_name = extract_gcs_tokens(full_object_name)
    if bucket_name == '' or object_name == '':
        logging.error("Full GCS object name is invalid. Aborting upload to "
                      "GCS.".format(local_path))
        return False

    # First HTTP request that initiates the upload.
    url = 'https://www.googleapis.com/upload/storage/v1/b/{0}' \
          '/o?uploadType=resumable&name={1}'.format(bucket_name, object_name)
    try:
        response = gcs_post_request(url)
        location = response.headers['Location']
        logging.debug(
            "Response Header Location (aka /upload URL): {0}".format(location))
    except requests.HTTPError as error:
        logging.error(
            "HTTPError on getting GCS session ID. Error: {0}".format(error))
        return False
    except KeyError as key_error:
        logging.error("KeyError on getting GCS session ID. Error: {0}.".format(
            key_error))
        return False

    # Actual file upload.
    new_url = location
    try:
        response = gcs_put_request(new_url, local_path)
        logging.debug("Final GCS response: {0}".format(str(response)))
    except requests.HTTPError as error:
        logging.error("Error on initial GCS upload".format(error))
        return False

    logging.info("Successfully uploaded '{0}' to GCS. "
                 "GCS object name is '{1}'.".format(local_path,
                                                    full_object_name))
    return True
Exemplo n.º 2
0
def upload_to_bucket(full_object_name, local_path):
  """ Uploads a file to GCS.

  Args:
    full_object_name: A str, a full GCS object name.
    local_path: A str, the path to a local backup file.

  Returns:
    True on success, False otherwise.
  """
  # Ensure local file is accessible.
  if not backup_recovery_helper.does_file_exist(local_path):
    logging.error("Local file '{0}' doesn't exist. Aborting upload to "
      "GCS.".format(local_path))
    return False

  # Extract bucket and object name for GCS.
  bucket_name, object_name = extract_gcs_tokens(full_object_name)
  if bucket_name == '' or object_name == '':
    logging.error("Full GCS object name is invalid. Aborting upload to "
      "GCS.".format(local_path))
    return False

  # First HTTP request that initiates the upload.
  url = 'https://www.googleapis.com/upload/storage/v1/b/{0}' \
        '/o?uploadType=resumable&name={1}'.format(bucket_name, object_name)
  try:
    response = gcs_post_request(url)
    location = response.headers['Location']
    logging.debug("Response Header Location (aka /upload URL): {0}".
      format(location))
  except requests.HTTPError as error:
    logging.error("HTTPError on getting GCS session ID. Error: {0}".
      format(error))
    return False
  except KeyError as key_error:
    logging.error("KeyError on getting GCS session ID. Error: {0}.".
      format(key_error))
    return False

  # Actual file upload.
  new_url = location
  try:
    response = gcs_put_request(new_url, local_path)
    logging.debug("Final GCS response: {0}".format(str(response)))
  except requests.HTTPError as error:
    logging.error("Error on initial GCS upload".format(error))
    return False

  logging.info("Successfully uploaded '{0}' to GCS. "
    "GCS object name is '{1}'.".format(local_path, full_object_name))
  return True
Exemplo n.º 3
0
 def test_does_file_exist(self):
   flexmock(os.path).should_receive('isfile').and_return().at_least().times(1)
   backup_recovery_helper.does_file_exist('foo')