GAE python: how to use delete_serving_url

时光怂恿深爱的人放手 提交于 2019-12-24 00:06:02

问题


  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:


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.



来源:https://stackoverflow.com/questions/38954000/gae-python-how-to-use-delete-serving-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!