如何对保存在 Google Cloud Store 中的文件使用 getServingUrl?

How do I use getServingUrl for a file saved in Google Cloud Store?

我正在使用 AppEngine (Java) 和 Google 云存储。该文件由存储桶中的应用程序保存(我已验证它有效)。这是我尝试获得一份服务的方式 URL:

filePath = "/gs/"+BUCKET_NAME+"/"+filename;
ServingUrlOptions options = ServingUrlOptions.Builder.withGoogleStorageFileName(filePath);
servingUrl = ImagesServiceFactory.getImagesService().getServingUrl(options);

我得到的错误并没有说明太多:

com.google.appengine.api.images.ImagesServiceFailureException: 
at com.google.appengine.api.images.ImagesServiceImpl.getServingUrl(ImagesServiceImpl.java:284)

文件路径正确(我已经检查过了,如果不正确,我会收到 "File not found" 错误)。

以下结果相同:

BlobKey blobKey = blobstoreService.createGsBlobKey(filePath);
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
ImagesServiceFactory.getImagesService().getServingUrl(options);

我正在使用云存储(而不是 blobstore),因为我希望能够设置文件和 "folder" 名称,这在使用 [=28 时似乎不可能=].

有什么想法吗?

即使将 blob 存储在存储桶中,也需要使用 blobkey 来提供图像。下面是对我有用的代码片段。

With Blobstore

            BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
            BlobInfo info = blobInfoFactory.loadBlobInfo(blobKey);
            ImagesService services = ImagesServiceFactory.getImagesService();
            ServingUrlOptions serve = ServingUrlOptions.Builder.withBlobKey(blobKey);
            String imageUrl = services.getServingUrl(serve); // This will give u image URL

您还可以在我已经在 Whosebug 上发布的三个答案中找到更多详细信息

upload a file using spring MultipartFile and google app engine

File missing in Google Cloud Storage on AppEngine after following instructions

How can I upload an thumbnail image (blob) at the same time as an Entity into a datastore in google app engine?

With GAE Bucket

UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucketName); // eg. mybucket

以下内容将 return 您 URL 将您的 blob 文件上传到 GAE Bucket。在这里您需要上传您的 blob 内容或图片。

String uploadUrl = blobstoreService.createUploadUrl("http://xxx.appspot.com/imageupload", uploadOptions);

使用 blobkey 提供图像

BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
BlobInfo info = blobInfoFactory.loadBlobInfo(blobKey);
ImagesService services = ImagesServiceFactory.getImagesService();
ServingUrlOptions serve = ServingUrlOptions.Builder.withBlobKey(blobKey);
String imageUrl = services.getServingUrl(serve);

原来题中的代码没有问题。保存在 GCS 中的文件除了主要内容外还包含一些元数据。因此,我需要使用 Apache Commons FileUpload 从负载中提取图像:

file = new ServletFileUpload().getItemIterator(request).next().openStream();