[英]Set Content-Type Azure Blob Storage
我目前正在编写一个后端,它将一个或多个图像/视频文件上传到 Azure Blob 存储中。 然而,我正在努力设置文件的内容类型。 Content-Type 默认设置为“application/octet-stream”,但我想通过使用 file.getContentType() 方法动态设置它们。
代码如下所示:
public void uploadToContainer(BlobClient blobClient, MultipartFile file) {
try {
blobClient.upload(file.getInputStream(), file.getSize());
} catch (Exception e) {
//TODO:
// Better error handling
e.printStackTrace();
}
}
有谁知道我怎么能做到这一点?
要在上传时设置 blob 的内容类型,您需要使用以下方法: uploadWithResponse(InputStream data, long length, ParallelTransferOptions parallelTransferOptions, BlobHttpHeaders headers, Map<String,String> metadata, AccessTier tier, BlobRequestConditions requestConditions, Duration timeout, Context context)
而不是您当前使用的upload
方法。
您将能够使用BlobHttpHeaders
定义内容类型。
在上传 JSON 文件时遇到了同样的问题,通过您当前使用的blobClient.upload
方法提出了这个问题:
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);
请注意,这是使用 azure-storage-blob v12.19.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.