gpt4 book ai didi

java - 即使启用版本控制,Google 云存储 Blob.delete() 也不会创建存档

转载 作者:行者123 更新时间:2023-12-02 10:26:04 25 4
gpt4 key购买 nike

我正在使用 Google Cloud Storage Java Api 来管理 Firebase 上的存储桶。我已使用以下命令激活了 gsutil 的版本控制:

gsutil versioning set on gs://[BUCKET_NAME]

之后我尝试用这个java代码删除一些文件:

com.google.cloud.storage.Bucket bucket = com.google.firebase.cloud.StorageClient.getInstance().bucket();
com.google.cloud.storage.Blob doc = bucket.get(documentPath);
log.info("Deleting document info => " + doc.toString());
doc.delete();

日志:删除文档信息 => Blob{bucket=[BUCKET_NAME], name=test.pdf, Generation=1545929848902866, size=196220, content-type=application/pdf,metadata=null}

这可行,但它会删除文件的实时版本和存档版本。

请问如何删除实时版本并保留存档版本?

最佳答案

versioning docs他们说:

If you send a delete request with a generation that corresponds to the currently live object, Cloud Storage deletes the object without making an archived copy.

因此,使用 bucket.get('test.pdf'); 返回文档及其生成编号(您可以在日志中看到它),即使我没有传递生成编号.

解决方案:

@Bean
public FirebaseApp provideFirebaseOptions() throws IOException {
log.info("INIT FIRBASE");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(apiKey.getInputStream()))
.setStorageBucket(bucketPath)
.build();
return FirebaseApp.initializeApp(options);
}
// add this bean to provide StorageOptions service
@Bean
public Storage provideStorageService() throws IOException {
log.info("INIT STORAGE");
return StorageOptions.newBuilder()
.setCredentials(ImplFirebaseTrampolines.getCredentials(provideFirebaseOptions()))
.build()
.getService();
}

获取并删除文档:

//autowire storage bean
private final Storage storage;
public StorageService(Storage storage) {this.storage = storage;}
...
BlobId blobId = BlobId.of(StorageClient.getInstance().bucket().getName(), documentPath);
log.info("Deleting document info => " + blobId.toString());
storage.delete(blobId);

关于java - 即使启用版本控制,Google 云存储 Blob.delete() 也不会创建存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53948704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com