使用Java删除后仍可从GCS存储桶下载文件的网址

问题描述 投票:0回答:1

所以从GCS存储桶删除文件时出现问题,我使用Java创建文件,代码如下:

public void upload(String projectId, String bucketName, String filePath, String fileName)
  throws IOException, URISyntaxException {

File f = new File(gcsCredDirectory+gcsCredFileName);
if (!f.exists()) {
  f.mkdirs();
}
try(InputStream is = new FileInputStream(f)) {
  StorageOptions storageOptions = StorageOptions.newBuilder()
      .setProjectId(projectId).setCredentials(fromStream(is)).build();
  Storage storage = storageOptions.getService();
  BlobId blobId = BlobId.of(bucketName, fileName);
  BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
  Blob result = storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
  URL url = storage.signUrl(blobInfo, MAX_EXPIRED_DATE, TimeUnit.DAYS, SignUrlOption.withV4Signature());
} catch (Exception e) {
  LOGGER.error("ERROR at GoogleCloudStorageServiceImpl.upload cause : ", e);
  throw e;
}

}

创建的代码运行得很好,我得到了一个URL,可以下载我上传的文件,实际上可以下载该文件,但是在通过以下代码删除文件后:

public boolean delete(String projectId, String bucketName, String fileName) {
    File f = new File(gcsCredDir+gcsCredFileName);
    if (!f.exists()) {
      f.mkdirs();
    }
    try(InputStream is = new FileInputStream(f)) {
      StorageOptions storageOptions = StorageOptions.newBuilder()
          .setProjectId(projectId)
          .setCredentials(fromStream(is))
          .build();
      boolean result = storageOptions.getService().delete(bucketName, fileName);
      LOGGER.info("Object " + fileName + " was deleted from " + bucketName);
      return result;
    } catch (Exception e) {
      return false;
    }
  }

我能够看到日志Object + fileName + was deleted from + bucketName,但是当我访问Url以下载文件时,我仍然可以下载它。我希望下载失败,因为文件已删除。

有什么建议吗?

谢谢

java google-cloud-platform google-cloud-storage bucket
1个回答
0
投票
Google拥有自己的缓存,这些缓存会在删除后存储您上传的内容一段时间。您需要在上传时使用HTTP标头覆盖设置。设置Cache-Control: max-age=0, no-cache。您还可以指定publicprivate. publicmeans intermediate servers may cache the result (for faster response times).private`,这意味着只有发出请求的客户端才应缓存响应,而不是中间服务器。

为了尝试强制缓存丢弃数据,某些服务器接受PURGE请求。这些可以通过curl -vv -X PURGE http(s)://example.com/path/to/resource

发布
© www.soinside.com 2019 - 2024. All rights reserved.