Search code examples
javagoogle-cloud-platformgoogle-cloud-storagebucket

Url to download file from GCS bucket still accessible after deleting using Java


So I have problem when deleting file from GCS bucket, I create my file using java, the code is like:

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;
}

}

The code to create went well, I get the Url to download the file I uploaded and actually can download the file, but after I deleting the file through this code:

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;
    }
  }

I was able to see the log Object + fileName + was deleted from + bucketName, but when I access the Url to download the file, I can still download it. I expect the download should failed because the file was deleted.

Any advice?

Thank you


Solution

  • Google has its own caches which will store what you upload for some time after you delete it. You need to override the settings using Headers on upload. Set Cache-Control: max-age=0, no-cache. You can also specify public or private.

    • public means intermediate servers may cache the result (for faster response times).
    • private means only the requesting client may cache the response, but not intermediate servers. This is usually set to enable a client to get a fresh copy each time the request is made.

    To try and force the cache to drop the data, some servers accept PURGE requests. These can be issued via curl -vv -X PURGE http(s)://example.com/path/to/resource

    Edit:

    You can set the cache control headers using gsutil: https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata