GAE python: how to use delete_serving_url

342 views Asked by At
  1. First I put image to storage:

    import cloudstorage as gcs 
    ... 
    path = '/bucket/folder/image.jpg'
    with gcs.open(path, 'w') as f:
        f.write(data)
    
  2. Then I get serving url:

    url = images.get_serving_url(None, filename='/gs{}'.format(self.path),
                                 secure_url=True)
    

    Serving url generally works as expected, the thing is I'm not using blob_key, only filename (path in storage).

  3. I wonder how to delete serving_url now, since sdk method only accepts blob_key

    def delete_serving_url(blob_key, rpc=None):
        """Delete a serving url that was created for a blob_key using get_serving_url.
    
        Args:
        blob_key: BlobKey, BlobInfo, str, or unicode representation of BlobKey of
        blob that has an existing URL to delete.
        rpc: Optional UserRPC object.
    
        Raises:
          BlobKeyRequiredError: when no blobkey was specified.
          InvalidBlobKeyError: the blob_key supplied was invalid.
          Error: There was a generic error deleting the serving url.
        """
    

https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.images#google.appengine.api.images.delete_serving_url

1

There are 1 answers

1
Dan Cornilescu On BEST ANSWER

The Using the Blobstore API with Google Cloud Storage example shows how to obtain an equivalent blob_key for GCS:

blob_key = CreateFile(main.BUCKET + '/blobstore_serving_demo')

From that link:

Note: Once you obtain a blobKey for the Google Cloud Storage object, you can pass it around, serialize it, and otherwise use it interchangeably anywhere you can use a blobKey for objects stored in Blobstore. This allows for usage where an app stores some data in blobstore and some in Google Cloud Storage, but treats the data otherwise identically by the rest of the app. (However, BlobInfo objects are not available for Google Cloud Storage objects.)

So you should be able to generate a blobKey for your file and call get_serving_url and delete_serving_url with it.

You could also use GCS object premissions to prevent access to the file, see Setting object permissions and metadata.