如何在不使用 Blob 的情况下使用 getServingUrl() 为图像创建基本 URL?到目前为止,我看到的所有示例都涉及 blob,但我需要使用 Google Cloud Storage 值。
谢谢。
如何在不使用 Blob 的情况下使用 getServingUrl() 为图像创建基本 URL?到目前为止,我看到的所有示例都涉及 blob,但我需要使用 Google Cloud Storage 值。
谢谢。
方法参数getServingUrl()有一个新的重载。ServingUrlOptions它的构建器有一个withGoogleStorageFileName()方法,您可以使用它来创建基于 Cloud Storage 文件名的 URL。
GcsFilename gcsFilename = new GcsFilename("bucketName", "objectName");
ImagesService is = ImagesServiceFactory.getImagesService();
String filename = String.format("/gs/%s/%s", gcsFilename.getBucketName(), gcsFilename.getObjectName());
String servingUrl = is.getServingUrl(ServingUrlOptions.Builder.withGoogleStorageFileName(filename));
Java文档:
这是一个老问题,但这是您从 Cloud Storage 提供图像的方式:
String key = "/gs/<bucket-name>/<path>"; // Such as /gs/example-bucket/categories/animals.png"
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions options = ServingUrlOptions.Builder
.withGoogleStorageFileName(key)
.imageSize(900) // Optional.
.crop(true); // Optional.
String servingUrl = imagesService.getServingUrl(options);
然后你可以对 URL 做任何你想做的事情。例如,您在那里重定向请求。
该 URL 可能有更多选项imageSize,crop但提供的 Java API 似乎不支持它。但是,您可以将其他选项附加到 URL 字符串,但这不是 GCS 的问题,而是图像 API 的问题。