[英]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.