如何获取 public link 本地开发服务器 google 云存储中上传文件的 public link(Google App engine+JAVA)

How to get public link for the uploaded file on google cloud storage in local dev server(Google App engine+JAVA)

我正在尝试在本地 google App Engine 开发服务器中使用 gcs 客户端库+java 上传图像文件。图像已成功上传,我可以在 localhost:8888/_ah/admin/datastore

下看到在本地数据存储中创建的条目

如何获取上传图像的 public 密钥,以便我可以在我的应用程序中显示图像。个人资料图片示例。

示例代码副本:

 String filePath = Constants.gcsUrl + "/" + bucketName + "/"+ objectName;
 GcsService gcsService = GcsServiceFactory
                .createGcsService(new RetryParams.Builder()
                        .initialRetryDelayMillis(10).retryMaxAttempts(10)
                        .totalRetryPeriodMillis(15000).build());
        GcsFilename filename = new GcsFilename(bucketName, objectName);
        GcsFileOptions options = new GcsFileOptions.Builder()
                .addUserMetadata("cache-control",
                        "max-age=" + (86400 * 365)).mimeType(mimeType)
                .acl(ACL_PUBLIC_READ).build();
        GcsOutputChannel writeChannel = gcsService.createOrReplace(
                filename, options);
        writeChannel.write(ByteBuffer.wrap(fileBytes));
        writeChannel.close();

您可以在本地开发服务器上使用图片服务。为此,您需要一个 blobkey:

BlobstoreService blobStore = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobStore.createGsBlobKey("/gs/" + filename.getBucketName() + "/" + filename.getObjectName())); // blobKey can be persisted
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(image));

通过附加 =sxxxx 调整大小也适用于本地开发服务器,但您无法获得原始图像大小 - 在生产中,将 =s0 附加到 url 得到原始图像图片,但它在本地不起作用。