Hi everyone, I have a system where a user has an in browser code editor. When they autosave / delete files / update content etc, I am updating the metadata (file_name, content_hash) etc in the DB, but the file contents themselves are updated in a storage bucket (e.g. S3, Google Cloud Storage, Local Filesystem).
In spring with @Transactional, the transaction rolls back if something goes wrong. But As far as i'm aware there is not a similar thing with storage buckets. I have so far been collecting the ids/paths of the uploaded stuff and in case it goes wrong i manually try delete / reverse the changes, but then theres the issue that maybe the delete fails too, etc.
I was wondering what the best way to do something like this is?
If it's at all helpful, i've been doing something like this so far (in Kotlin):
override fun uploadList(req: StoragePutRequestList): UploadedPaths {
val uploaded = mutableListOf<String>()
try {
req.requests.forEach { req ->
uploadData(bucketName, req)
uploaded.add(req.path)
}
} catch (exception: Exception) {
rollbackAdditions(bucketName, uploaded)
throw exception
}
return UploadedPaths(uploaded)
}
private fun uploadData (bucketName: String, request: StoragePutRequest) {
val blobId = BlobId.of(bucketName, request.path)
val blobInfo = BlobInfo.newBuilder(blobId)
.setContentType("text/plain")
.build()
storage.create(blobInfo, request.content.toByteArray(Charsets.UTF_8))
}
private fun rollbackAdditions (bucket: String, uploaded: List<String>) {
uploaded.forEach { path -> storage.delete(bucket, path) }
}