【问题标题】:How do I use getServingUrl for a file saved in Google Cloud Store?如何将 getServingUrl 用于保存在 Google Cloud Store 中的文件?
【发布时间】:2016-03-24 01:24:30
【问题描述】:

我正在使用 AppEngine (Java) 和 Google Cloud Storage。该文件由应用程序保存在存储桶中(我已经验证它可以工作)。以下是我尝试获取服务 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)

文件路径正确(我已经检查过了,如果不正确,我会收到“找不到文件”错误)。

以下导致相同的结果:

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

我正在使用云存储(而不是 blobstore),因为我希望能够设置文件和“文件夹”名称,这在使用 blobstoreService.createUploadUrl 时似乎是不可能的。

有什么想法吗?

【问题讨论】:

    标签: java google-app-engine google-cloud-storage


    【解决方案1】:

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

    使用 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
    

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

    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?

    使用 GAE 存储桶

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

    以下内容将返回用于将 blob 文件上传到 GAE Bucket 的 URL。在这里您需要上传您的 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);
    

    【讨论】:

    • 谢谢,当我将内容保存在 blobstore 中时,它可以工作,但不能保存在云存储中。使用 blobstoreService.createGsBlobKey 会导致同样的错误。
    • @swhy 这是我的存储桶的工作代码。还要检查我共享的链接,其中包含有关存储桶权限的更多信息。
    • 感谢 Ankur。这可行,但它会创建两个副本,一个在 blobstore 中,一个在存储桶中。是否可以为 Cloud Storage 中的某些内容创建服务 URL。我希望能够设置文件和“文件夹”名称,这在使用 createUploadUrl 时似乎是不可能的。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多