GAE python: 如何使用 delete_serving_url

GAE python: how to use delete_serving_url

  1. 首先我将图像存储:

    import cloudstorage as gcs 
    ... 
    path = '/bucket/folder/image.jpg'
    with gcs.open(path, 'w') as f:
        f.write(data)
    
  2. 然后我开始服务url:

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

    服务 url 通常按预期工作,问题是我没有使用 blob_key,仅使用文件名(存储路径)。

  3. 我想知道现在如何删除serving_url,因为sdk方法只接受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

Using the Blobstore API with Google Cloud Storage 示例显示了如何为 GCS 获得等效的 blob_key:

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

来自 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.)

所以您应该能够为您的文件生成一个 blobKey 并用它调用 get_serving_urldelete_serving_url

您还可以使用 GCS 对象权限来阻止对文件的访问,请参阅 Setting object permissions and metadata