Reputation: 33
I am currently writing a backend which takes in one or many image/video-files to be uploaded into Azure Blob Storage. I am however struggling to set the Content-Type of the files. The Content-Type is by default set to be "application/octet-stream", but I want to dynamically set them by using the file.getContentType() method.
The code looks like this:
public void uploadToContainer(BlobClient blobClient, MultipartFile file) {
try {
blobClient.upload(file.getInputStream(), file.getSize());
} catch (Exception e) {
//TODO:
// Better error handling
e.printStackTrace();
}
}
Does anyone know how I can accomplish this?
Upvotes: 3
Views: 2742
Reputation: 51
Faced the same issue uploading JSON file, came up with this from stepping through the
blobClient.upload
method you're current using:
BlobHttpHeaders jsonHeaders = new BlobHttpHeaders()
.setContentType(MediaType.APPLICATION_JSON_VALUE);
BinaryData data = BinaryData.fromStream(file.getInputStream(), file.getSize());
BlobParallelUploadOptions options = new BlobParallelUploadOptions(data)
.setRequestConditions(new BlobRequestConditions()).setHeaders(jsonHeaders);
blobClient.uploadWithResponse(options, null, Context.NONE);
Note this is using azure-storage-blob v12.19.0
Upvotes: 4
Reputation: 136186
To set content type of a blob at the time of uploading, you will need to use the following method: uploadWithResponse(InputStream data, long length, ParallelTransferOptions parallelTransferOptions, BlobHttpHeaders headers, Map<String,String> metadata, AccessTier tier, BlobRequestConditions requestConditions, Duration timeout, Context context)
instead of upload
method that you're using currently.
You will be able to define content type using BlobHttpHeaders
.
Upvotes: 1