google cloud storage检查对象是否存在(App Engine Java)

google cloud storage检查对象是否存在(App Engine Java)

问题描述:

我的AppEngine项目有一个API方法,该方法将可恢复的URL发送到Android客户端,然后该客户端使用该可恢复的URL上传图像.

My AppEngine project has an API method that sends a resumable URL to an Android Client which then uses that resumable URL to upload an image.

我还有另一个API方法,可以创建并返回签名的URL.在签名的URL"中,您必须指定Google Cloud Storage存储桶和对象名称.但是,该对象可能不存在,在这种情况下,签名的URL当然将不起作用.

I have another API method that create and returns a signed URL. In the Signed URL you must specify the Google Cloud Storage bucket and object name. However, that object may not exist, in which case, the signed URL will of course not work.

在发布签名URL之前,如何快速检查App Engine后端中是否存在对象(在存储桶中)?

How can I quickly check if an object exists (in a bucket) in my App Engine backend before issuing the Signed URL?

我的App Engine项目是一个Cloud Endpoints项目.

My App Engine project is a Cloud Endpoints project.

您可以调用 getMetadata 来检查对象是否存在,而无需下载它.

You can call getMetadata to check if the object exists without downloading it.

GcsService fileService = GcsServiceFactory.createGcsService();
GcsFilename file = new GcsFilename(bucket, object);
fileService.getMetadata(file);

或者,您可以列出存储桶中的所有对象,也可以列出存储桶中以指定前缀(必要时可以等于对象名称)开头的所有对象.

Alternatively, you can list all objects in a bucket or all objects in a bucket that start with the specified prefix (which can equal the object's name, if necessary).

更新:

这是我将uploadURL发送给客户端的方式:

This is how I send uploadURL to my client:

@Override
public String getUploadUrl() throws LoginException, VersionException {
    // Verify that call is from a registered user and with proper headers

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    String callbackUrl = "/blob";
    return blobstoreService.createUploadUrl(callbackUrl,
           UploadOptions.Builder.withGoogleStorageBucketName("myBucket));
}