Ejemplo n.º 1
0
    def delete_batch(self, paths):
        """Deletes the objects at the given GCS paths.

    Args:
      paths: List of GCS file path patterns in the form gs://<bucket>/<name>,
             not to exceed MAX_BATCH_OPERATION_SIZE in length.

    Returns: List of tuples of (path, exception) in the same order as the paths
             argument, where exception is None if the operation succeeded or
             the relevant exception if the operation failed.
    """
        if not paths:
            return []
        batch_request = BatchApiRequest(
            batch_url=GCS_BATCH_ENDPOINT,
            retryable_codes=retry.SERVER_ERROR_OR_TIMEOUT_CODES)
        for path in paths:
            bucket, object_path = parse_gcs_path(path)
            request = storage.StorageObjectsDeleteRequest(bucket=bucket,
                                                          object=object_path)
            batch_request.Add(self.client.objects, 'Delete', request)
        api_calls = batch_request.Execute(self.client._http)  # pylint: disable=protected-access
        result_statuses = []
        for i, api_call in enumerate(api_calls):
            path = paths[i]
            exception = None
            if api_call.is_error:
                exception = api_call.exception
                # Return success when the file doesn't exist anymore for idempotency.
                if isinstance(exception,
                              HttpError) and exception.status_code == 404:
                    exception = None
            result_statuses.append((path, exception))
        return result_statuses
Ejemplo n.º 2
0
    def delete(self, path):
        """Deletes the object at the given GCS path.

    Args:
      path: GCS file path pattern in the form gs://<bucket>/<name>.
    """
        bucket, object_path = parse_gcs_path(path)
        request = storage.StorageObjectsDeleteRequest(bucket=bucket,
                                                      object=object_path)
        try:
            self.client.objects.Delete(request)
        except HttpError as http_error:
            if http_error.status_code == 404:
                # Return success when the file doesn't exist anymore for idempotency.
                return
            raise