Python google.cloud.exceptions.NotFound() Examples

The following are 30 code examples of google.cloud.exceptions.NotFound(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.cloud.exceptions , or try the search function .
Example #1
Source File: test_notification.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_delete_miss(self):
        from google.cloud.exceptions import NotFound

        client = self._make_client()
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID
        api_request = client._connection.api_request
        api_request.side_effect = NotFound("testing")

        with self.assertRaises(NotFound):
            notification.delete(timeout=42)

        api_request.assert_called_once_with(
            method="DELETE", path=self.NOTIFICATION_PATH, query_params={}, timeout=42
        ) 
Example #2
Source File: gcloud_storage_driver.py    From surround with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def pull(self, remote_path, local_path=None, override_ok=False):
        blob = self.bucket.blob(join_path(self.base_path, remote_path))

        data = None
        try:
            data = blob.download_as_string()
        except NotFound:
            raise FileNotFoundError("That file doesn't exist")

        if local_path:
            if not override_ok and os.path.exists(local_path):
                raise FileExistsError("File already exists at pull location!")

            with open(local_path, "wb+") as f:
                f.write(data)

            return True

        return data 
Example #3
Source File: bigquery.py    From airflow with Apache License 2.0 6 votes vote down vote up
def table_exists(self, dataset_id: str, table_id: str, project_id: str) -> bool:
        """
        Checks for the existence of a table in Google BigQuery.

        :param project_id: The Google cloud project in which to look for the
            table. The connection supplied to the hook must provide access to
            the specified project.
        :type project_id: str
        :param dataset_id: The name of the dataset in which to look for the
            table.
        :type dataset_id: str
        :param table_id: The name of the table to check the existence of.
        :type table_id: str
        """
        table_reference = TableReference(DatasetReference(project_id, dataset_id), table_id)
        try:
            self.get_client().get_table(table_reference)
            return True
        except NotFound:
            return False 
Example #4
Source File: hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def delete(self, timeout=_DEFAULT_TIMEOUT):
        """Delete the key from Cloud Storage.

        :type timeout: float or tuple
        :param timeout: (Optional) The amount of time, in seconds, to wait
            for the server response.

            Can also be passed as a tuple (connect_timeout, read_timeout).
            See :meth:`requests.Session.request` documentation for details.

        :raises :class:`~google.api_core.exceptions.NotFound`:
            if the key does not exist on the back-end.
        """
        if self.state != self.INACTIVE_STATE:
            raise ValueError("Cannot delete key if not in 'INACTIVE' state.")

        qs_params = {}
        if self.user_project is not None:
            qs_params["userProject"] = self.user_project

        self._client._connection.api_request(
            method="DELETE", path=self.path, query_params=qs_params, timeout=timeout
        ) 
Example #5
Source File: gcloud_storage_driver.py    From surround with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def delete(self, remote_path):
        blob = self.bucket.blob(join_path(self.base_path, remote_path))

        try:
            blob.delete()
        except NotFound:
            remote_path = normalize_path(remote_path)
            blobs = self.client.list_blobs(self.bucket, prefix=remote_path)

            is_empty = True
            for b in blobs:
                b.delete()
                is_empty = False

            if is_empty:
                raise FileNotFoundError("Could not find the file/folder to delete!") 
Example #6
Source File: googleJobStore.py    From toil with Apache License 2.0 6 votes vote down vote up
def destroy(self):
        try:
            self.bucket = self.storageClient.get_bucket(self.bucketName)

        except exceptions.NotFound:
            # just return if not connect to physical storage. Needed for idempotency
            return
        try:
            self.bucket.delete(force=True)
            # throws ValueError if bucket has more than 256 objects. Then we must delete manually
        except ValueError:
            self.bucket.delete_blobs(self.bucket.list_blobs())
            self.bucket.delete()
            # if ^ throws a google.cloud.exceptions.Conflict, then we should have a deletion retry mechanism.

        # google freaks out if we call delete multiple times on the bucket obj, so after success
        # just set to None.
        self.bucket = None 
Example #7
Source File: http.py    From open-raadsinformatie with MIT License 6 votes vote down vote up
def download_cache(self, path):
        """Retrieve and return the cached file"""
        bucket = self.get_bucket()
        blob = bucket.get_blob(path)

        if not blob:
            raise NotFound(path)

        media_file = io.BytesIO()
        self._do_download(blob, media_file)
        media_file.seek(0, 0)
        log.debug('Retrieved file from GCS bucket %s: %s' % (self.bucket_name, path))

        resource = FileResource(media_file)
        resource.content_type = blob.content_type
        resource.file_size = blob.size
        return resource 
Example #8
Source File: table_exists.py    From python-bigquery with Apache License 2.0 6 votes vote down vote up
def table_exists(table_id):

    # [START bigquery_table_exists]
    from google.cloud import bigquery
    from google.cloud.exceptions import NotFound

    client = bigquery.Client()

    # TODO(developer): Set table_id to the ID of the table to determine existence.
    # table_id = "your-project.your_dataset.your_table"

    try:
        client.get_table(table_id)  # Make an API request.
        print("Table {} already exists.".format(table_id))
    except NotFound:
        print("Table {} is not found.".format(table_id))
    # [END bigquery_table_exists] 
Example #9
Source File: __init__.py    From jgscm with MIT License 6 votes vote down vote up
def list_checkpoints(self, path):
        """Return a list of checkpoints for a given file"""
        cp = self._get_checkpoint_path(None, path)
        bucket_name, bucket_path = self.parent._parse_path(cp)
        try:
            bucket = self.parent._get_bucket(bucket_name)
            it = bucket.list_blobs(prefix=bucket_path, delimiter="/",
                                   max_results=self.parent.max_list_size)
            checkpoints = [{
                "id": os.path.splitext(file.path)[0][-36:],
                "last_modified": file.updated,
            } for file in islice(it, self.parent.max_list_size)]
        except NotFound:
            return []
        checkpoints.sort(key=lambda c: c["last_modified"], reverse=True)
        self.log.debug("list_checkpoints: %s: %s", path, checkpoints)
        return checkpoints 
Example #10
Source File: table_exists.py    From python-bigquery with Apache License 2.0 6 votes vote down vote up
def table_exists(table_id):

    # [START bigquery_table_exists]
    from google.cloud import bigquery
    from google.cloud.exceptions import NotFound

    client = bigquery.Client()

    # TODO(developer): Set table_id to the ID of the table to determine existence.
    # table_id = "your-project.your_dataset.your_table"

    try:
        client.get_table(table_id)  # Make an API request.
        print("Table {} already exists.".format(table_id))
    except NotFound:
        print("Table {} is not found.".format(table_id))
    # [END bigquery_table_exists] 
Example #11
Source File: storage.py    From loaner with Apache License 2.0 6 votes vote down vote up
def get_bucket(self, bucket_name=None):
    """Retrieves a Google Cloud Storage Bucket object.

    Args:
      bucket_name: str, the name of the Google Cloud Storage Bucket to retrieve.

    Returns:
      A dictionary object representing a Google Cloud Storage Bucket.
          type: google.cloud.storage.bucket.Bucket

    Raises:
      NotFoundError: when a resource is not found.
    """
    bucket_name = bucket_name or self._config.bucket
    try:
      return self._client.get_bucket(bucket_name)
    except exceptions.NotFound as err:
      logging.error(_GET_BUCKET_ERROR_MSG, bucket_name, err)
      raise NotFoundError(_GET_BUCKET_ERROR_MSG % (bucket_name, err)) 
Example #12
Source File: dataset_exists.py    From python-bigquery with Apache License 2.0 6 votes vote down vote up
def dataset_exists(dataset_id):

    # [START bigquery_dataset_exists]
    from google.cloud import bigquery
    from google.cloud.exceptions import NotFound

    client = bigquery.Client()

    # TODO(developer): Set dataset_id to the ID of the dataset to determine existence.
    # dataset_id = "your-project.your_dataset"

    try:
        client.get_dataset(dataset_id)  # Make an API request.
        print("Dataset {} already exists".format(dataset_id))
    except NotFound:
        print("Dataset {} is not found".format(dataset_id))
    # [END bigquery_dataset_exists] 
Example #13
Source File: hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def update(self, timeout=_DEFAULT_TIMEOUT):
        """Save writable properties to Cloud Storage.

        :type timeout: float or tuple
        :param timeout: (Optional) The amount of time, in seconds, to wait
            for the server response.

            Can also be passed as a tuple (connect_timeout, read_timeout).
            See :meth:`requests.Session.request` documentation for details.

        :raises :class:`~google.api_core.exceptions.NotFound`:
            if the key does not exist on the back-end.
        """
        qs_params = {}
        if self.user_project is not None:
            qs_params["userProject"] = self.user_project

        payload = {"state": self.state}
        self._properties = self._client._connection.api_request(
            method="PUT",
            path=self.path,
            data=payload,
            query_params=qs_params,
            timeout=timeout,
        ) 
Example #14
Source File: gcs.py    From ctfscoreboard with Apache License 2.0 6 votes vote down vote up
def send(attachment):
    """Send to download URI."""
    try:
        client = storage.Client()
        bucket = client.bucket(get_bucket())
        buf = BytesIO()
        blob = bucket.get_blob(attachment.aid)
        if not blob:
            return flask.abort(404)
        blob.download_to_file(buf)
        buf.seek(0)
        return flask.send_file(
            buf,
            mimetype=attachment.content_type,
            attachment_filename=attachment.filename,
            add_etags=False, as_attachment=True)
    except exceptions.NotFound:
        return flask.abort(404) 
Example #15
Source File: hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def reload(self, timeout=_DEFAULT_TIMEOUT):
        """Reload properties from Cloud Storage.

        :type timeout: float or tuple
        :param timeout: (Optional) The amount of time, in seconds, to wait
            for the server response.

            Can also be passed as a tuple (connect_timeout, read_timeout).
            See :meth:`requests.Session.request` documentation for details.

        :raises :class:`~google.api_core.exceptions.NotFound`:
            if the key does not exist on the back-end.
        """
        qs_params = {}

        if self.user_project is not None:
            qs_params["userProject"] = self.user_project

        self._properties = self._client._connection.api_request(
            method="GET", path=self.path, query_params=qs_params, timeout=timeout
        ) 
Example #16
Source File: translate_v3_list_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # clean up
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #17
Source File: translate_v3_get_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # cleanup
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #18
Source File: translate_v3_translate_text_with_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # cleanup
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #19
Source File: translate_v3_create_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def test_create_glossary(capsys):
    try:
        glossary_id = "test-{}".format(uuid.uuid4())
        translate_v3_create_glossary.create_glossary(
            PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
        )
        out, _ = capsys.readouterr()
        # assert
        assert "Created:" in out
        assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
    finally:
        # cleanup
        @backoff.on_exception(
            backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
        )
        def delete_glossary():
            try:
                translate_v3_delete_glossary.delete_glossary(
                    PROJECT_ID, glossary_id)
            except NotFound as e:
                # Ignoring this case.
                print("Got NotFound, detail: {}".format(str(e)))
        delete_glossary() 
Example #20
Source File: snippets.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def delete_blob(to_delete):
    # [START delete_blob]
    from google.cloud.exceptions import NotFound

    client = storage.Client()
    bucket = client.get_bucket("my-bucket")
    blobs = list(bucket.list_blobs())
    assert len(blobs) > 0
    # [<Blob: my-bucket, my-file.txt>]
    bucket.delete_blob("my-file.txt")
    try:
        bucket.delete_blob("doesnt-exist")
    except NotFound:
        pass
    # [END delete_blob]

    blob = None
    # [START delete_blobs]
    bucket.delete_blobs([blob], on_error=lambda blob: None)
    # [END delete_blobs]

    to_delete.append(bucket) 
Example #21
Source File: test_hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_exists_miss_no_project_set(self):
        from google.cloud.exceptions import NotFound

        access_id = "ACCESS-ID"
        connection = mock.Mock(spec=["api_request"])
        connection.api_request.side_effect = NotFound("testing")
        client = _Client(connection)
        metadata = self._make_one(client)
        metadata._properties["accessId"] = access_id

        self.assertFalse(metadata.exists(timeout=42))

        expected_path = "/projects/{}/hmacKeys/{}".format(
            client.DEFAULT_PROJECT, access_id
        )
        expected_kwargs = {
            "method": "GET",
            "path": expected_path,
            "query_params": {},
            "timeout": 42,
        }
        connection.api_request.assert_called_once_with(**expected_kwargs) 
Example #22
Source File: test_hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_reload_miss_no_project_set(self):
        from google.cloud.exceptions import NotFound

        access_id = "ACCESS-ID"
        connection = mock.Mock(spec=["api_request"])
        connection.api_request.side_effect = NotFound("testing")
        client = _Client(connection)
        metadata = self._make_one(client)
        metadata._properties["accessId"] = access_id

        with self.assertRaises(NotFound):
            metadata.reload(timeout=42)

        expected_path = "/projects/{}/hmacKeys/{}".format(
            client.DEFAULT_PROJECT, access_id
        )
        expected_kwargs = {
            "method": "GET",
            "path": expected_path,
            "query_params": {},
            "timeout": 42,
        }
        connection.api_request.assert_called_once_with(**expected_kwargs) 
Example #23
Source File: test_hmac_key.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_update_miss_no_project_set(self):
        from google.cloud.exceptions import NotFound

        access_id = "ACCESS-ID"
        connection = mock.Mock(spec=["api_request"])
        connection.api_request.side_effect = NotFound("testing")
        client = _Client(connection)
        metadata = self._make_one(client)
        metadata._properties["accessId"] = access_id
        metadata.state = "INACTIVE"

        with self.assertRaises(NotFound):
            metadata.update(timeout=42)

        expected_path = "/projects/{}/hmacKeys/{}".format(
            client.DEFAULT_PROJECT, access_id
        )
        expected_kwargs = {
            "method": "PUT",
            "path": expected_path,
            "data": {"state": "INACTIVE"},
            "query_params": {},
            "timeout": 42,
        }
        connection.api_request.assert_called_once_with(**expected_kwargs) 
Example #24
Source File: test_bucket.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_exists_miss(self):
        from google.cloud.exceptions import NotFound

        class _FakeConnection(object):

            _called_with = []

            @classmethod
            def api_request(cls, *args, **kwargs):
                cls._called_with.append((args, kwargs))
                raise NotFound(args)

        BUCKET_NAME = "bucket-name"
        bucket = self._make_one(name=BUCKET_NAME)
        client = _Client(_FakeConnection)
        self.assertFalse(bucket.exists(client=client, timeout=42))
        expected_called_kwargs = {
            "method": "GET",
            "path": bucket.path,
            "query_params": {"fields": "name"},
            "_target_object": None,
            "timeout": 42,
        }
        expected_cw = [((), expected_called_kwargs)]
        self.assertEqual(_FakeConnection._called_with, expected_cw) 
Example #25
Source File: test_bucket.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_delete_miss(self):
        from google.cloud.exceptions import NotFound

        NAME = "name"
        connection = _Connection()
        client = _Client(connection)
        bucket = self._make_one(client=client, name=NAME)
        self.assertRaises(NotFound, bucket.delete)
        expected_cw = [
            {
                "method": "DELETE",
                "path": bucket.path,
                "query_params": {},
                "_target_object": None,
                "timeout": self._get_default_timeout(),
            }
        ]
        self.assertEqual(connection._deleted_buckets, expected_cw) 
Example #26
Source File: test_bucket.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_delete_blobs_miss_no_on_error(self):
        from google.cloud.exceptions import NotFound

        NAME = "name"
        BLOB_NAME = "blob-name"
        NONESUCH = "nonesuch"
        connection = _Connection({})
        client = _Client(connection)
        bucket = self._make_one(client=client, name=NAME)
        self.assertRaises(NotFound, bucket.delete_blobs, [BLOB_NAME, NONESUCH])
        kw = connection._requested
        self.assertEqual(len(kw), 2)
        self.assertEqual(kw[0]["method"], "DELETE")
        self.assertEqual(kw[0]["path"], "/b/%s/o/%s" % (NAME, BLOB_NAME))
        self.assertEqual(kw[0]["timeout"], self._get_default_timeout())
        self.assertEqual(kw[1]["method"], "DELETE")
        self.assertEqual(kw[1]["path"], "/b/%s/o/%s" % (NAME, NONESUCH))
        self.assertEqual(kw[1]["timeout"], self._get_default_timeout()) 
Example #27
Source File: test_notification.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_reload_miss(self):
        from google.cloud.exceptions import NotFound

        client = self._make_client()
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID
        api_request = client._connection.api_request
        api_request.side_effect = NotFound("testing")

        with self.assertRaises(NotFound):
            notification.reload(timeout=42)

        api_request.assert_called_once_with(
            method="GET", path=self.NOTIFICATION_PATH, query_params={}, timeout=42
        ) 
Example #28
Source File: translate_v3_batch_translate_text_with_glossary_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def glossary():
    """Get the ID of a glossary available to session (do not mutate/delete)."""
    glossary_id = "test-{}".format(uuid.uuid4())
    translate_v3_create_glossary.create_glossary(
        PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
    )

    yield glossary_id

    # cleanup
    @backoff.on_exception(
        backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
    )
    def delete_glossary():
        try:
            translate_v3_delete_glossary.delete_glossary(
                PROJECT_ID, glossary_id)
        except NotFound as e:
            # Ignoring this case.
            print("Got NotFound, detail: {}".format(str(e)))
    delete_glossary() 
Example #29
Source File: test_system.py    From python-storage with Apache License 2.0 5 votes vote down vote up
def test_lifecycle_rules(self):
        from google.cloud.storage import constants

        new_bucket_name = "w-lifcycle-rules" + unique_resource_id("-")
        self.assertRaises(
            exceptions.NotFound, Config.CLIENT.get_bucket, new_bucket_name
        )
        bucket = Config.CLIENT.bucket(new_bucket_name)
        bucket.add_lifecycle_delete_rule(age=42)
        bucket.add_lifecycle_set_storage_class_rule(
            constants.COLDLINE_STORAGE_CLASS,
            is_live=False,
            matches_storage_class=[constants.NEARLINE_STORAGE_CLASS],
        )

        expected_rules = [
            LifecycleRuleDelete(age=42),
            LifecycleRuleSetStorageClass(
                constants.COLDLINE_STORAGE_CLASS,
                is_live=False,
                matches_storage_class=[constants.NEARLINE_STORAGE_CLASS],
            ),
        ]

        retry_429_503(bucket.create)(location="us")

        self.case_buckets_to_delete.append(new_bucket_name)
        self.assertEqual(bucket.name, new_bucket_name)
        self.assertEqual(list(bucket.lifecycle_rules), expected_rules)

        bucket.clear_lifecyle_rules()
        bucket.patch()

        self.assertEqual(list(bucket.lifecycle_rules), []) 
Example #30
Source File: test_table_util.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def test_create_table(self):
        """Tests TableUtil.create_table().

        Tests TableUtil's ability to translate create a table in the specified
        dataset.

        Returns:
            True if test passes, else False.
        """
        self.table_util.create_table()
        try:
            self.bq_client.get_table(self.table_util.table)
        except NotFound:
            self.fail("Table not created.")