0
votes

I am using Google Cloud Storage Java Api to manage my bucket on Firebase. i have activated the versioning with gsutil using this command :

gsutil versioning set on gs://[BUCKET_NAME]

After that i tried to delete some files with this java code :

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();

Log : Deleting document info => Blob{bucket=[BUCKET_NAME], name=test.pdf, generation=1545929848902866, size=196220, content-type=application/pdf, metadata=null}

This works, but it removes both the live and the archived version of the file.

Please how to just delete the live version and conserve the archived one ?

2

2 Answers

1
votes

In the versioning docs they say :

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.

So, using bucket.get('test.pdf'); returns the document and its generation number (you can see it in the log) even if i don't pass the generation number.

Solution :

@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();
}

Get and delete the doc :

//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);
0
votes

You are turning versioning off. The correct command to enable versioning is:

gsutil versioning set on gs://[BUCKET_NAME]