google-app-engine - 谷歌云存储 - getServingUrl() 给出旧图像的 URL

标签 google-app-engine google-cloud-storage google-cloud-platform

我正在 GCS 上上传图像,然后用具有相同文件名的不同图像 (image.png) 覆盖它。在 GCS 浏览器中,我可以看到图像被覆盖(不同的大小、时间戳),但是当我尝试使用 getServingUrl() 获取图像的 URL 时,我得到了我第一次上传的图像的 URL!

如果我添加具有新文件名的图像 (image2.png),则会得到一个新 URL。如果我用另一张图像覆盖此图像(保留名称为 image2.png),那么我将再次获得原始 image2.png 的 URL。

这是我的代码:

String bucketName = "my_bucket_name";
    String userFileName = "image.png";
    try {
        GcsFileOptions instance = GcsFileOptions.getDefaultInstance();
        GcsFilename fileName = new GcsFilename(bucketName, userFileName);// getFileName(request);
        GcsOutputChannel outputChannel;
        gcsService.delete(fileName);
        outputChannel = gcsService.createOrReplace(fileName, instance);
        copy(request.getInputStream(), Channels.newOutputStream(outputChannel));
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

    ImagesService imagesService = ImagesServiceFactory.getImagesService();

    String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" +
            bucketName + "/" + userFileName).secureUrl(true));

谁能告诉我我在这里缺少什么吗?如何让 ImagesService 为我提供新上传图像的句柄?谢谢。

最佳答案

这是预期的。该对象可以缓存在您无法控制的任何中间代理服务器中。因此,设置缓存控制 header 也可能无法解决问题。

上传文件时,您可以使用文件的 md5hash 作为名称,而不是使用文件名保存。这将确保如果文件内容发生更改,您将拥有不同的 md5hash。通过这种方法,您还可以使用缓存控制 header ,这将加快网页的加载速度

关于google-app-engine - 谷歌云存储 - getServingUrl() 给出旧图像的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671177/

相关文章:

google-app-engine - 多区域 Cloud Storage 费用

python - Web/Screen Scraping with Google App Engine - 代码在 python 解释器中工作,但在 GAE 中不工作

java - 如何在 App Engine Java 中配置 IP 地址和端口?

java - 如何使用 Google App Engine 邮件服务发送电子邮件?

database - Google Cloud Spanner 是否支持存储过程?

rest - GCP Billing API 的响应中缺少 Compute Engine 规范信息

google-cloud-platform - 调度数据流管道

google-app-engine - Google App Engine ndb 在重复属性上的性能

python - 为什么upload_from_file Google Cloud Storage Function 会引发超时错误?

google-cloud-storage - 从 Google Cloud Storage 恢复以前版本的文件