Url 使用 Java 删除后仍然可以从 GCS 存储桶下载文件
Url to download file from GCS bucket still accessible after deleting using Java
所以我在从 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下载文件时,我仍然可以下载它。我预计下载应该会失败,因为文件已被删除。
有什么建议吗?
谢谢
Google 有自己的缓存,可以在您删除上传的内容后保存一段时间。您需要在上传时使用 Headers 覆盖设置。设置 Cache-Control: max-age=0, no-cache
。您还可以指定 public
或 private
。
public
意味着中间服务器可能缓存结果(为了更快的响应时间)。
private
意味着只有请求客户端可以缓存响应,但不能缓存中间服务器。这通常设置为使客户端能够在每次发出请求时获得新副本。
为了尝试强制缓存删除数据,一些服务器接受 PURGE
请求。这些可以通过 curl -vv -X PURGE http(s)://example.com/path/to/resource
发布
编辑:
您可以使用 gsutil 设置缓存控制 headers:https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata
所以我在从 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下载文件时,我仍然可以下载它。我预计下载应该会失败,因为文件已被删除。
有什么建议吗?
谢谢
Google 有自己的缓存,可以在您删除上传的内容后保存一段时间。您需要在上传时使用 Headers 覆盖设置。设置 Cache-Control: max-age=0, no-cache
。您还可以指定 public
或 private
。
public
意味着中间服务器可能缓存结果(为了更快的响应时间)。private
意味着只有请求客户端可以缓存响应,但不能缓存中间服务器。这通常设置为使客户端能够在每次发出请求时获得新副本。
为了尝试强制缓存删除数据,一些服务器接受 PURGE
请求。这些可以通过 curl -vv -X PURGE http(s)://example.com/path/to/resource
编辑:
您可以使用 gsutil 设置缓存控制 headers:https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata