This seems to me to be a simple usage of the storage API, but I cannot find an example. If we have a given directory and filename, and want to create a file at that location if it does not exist, or replace it (with likely changed length) if it does, how do you do that? That infallible resource ChatGPT says there is no way to change the length directly.
Here is what I have:
fun ShareFileClientBuilder.saveToStorage(channel: java.io.InputStream, size: Long, dirName: String, fileBaseName: String) {
this.createDirectoryPath(dirName)
val fileClient = this.resourcePath(dirName).buildDirectoryClient().getFileClient(fileBaseName)
if (fileClient.exists()) {
fileClient.uploadRange(channel, size) // <== THIS DOES NOT CHANGE THE SIZE
} else {
fileClient.create(size)
fileClient.upload(channel, size, ParallelTransferOptions())
}
}
That elicits this response from Azure:
The range specified is invalid for the current size of the resource
This is not surprising since the new content is likely not the same length as the old.
Instead:
if (!fileClient.exists()) {
fileClient.create(size)
}
fileClient.upload(channel, size, ParallelTransferOptions())
doesn’t change the error if the file already exists (unsurprisingly).
Surely there must be some way of changing the length of the resource? There is ShareFileClient.setProperties
but that goes further down the rabbit-hole by requiring you to provide other data at the same time, some of which I don’t know how to obtain.
1