简体   繁体   English

设置 Content-Type Azure Blob 存储

[英]Set Content-Type Azure Blob Storage

I am currently writing a backend which takes in one or many image/video-files to be uploaded into Azure Blob Storage.我目前正在编写一个后端,它将一个或多个图像/视频文件上传到 Azure Blob 存储中。 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. Content-Type 默认设置为“application/octet-stream”,但我想通过使用 file.getContentType() 方法动态设置它们。

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?有谁知道我怎么能做到这一点?

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.要在上传时设置 blob 的内容类型,您需要使用以下方法: uploadWithResponse(InputStream data, long length, ParallelTransferOptions parallelTransferOptions, BlobHttpHeaders headers, Map<String,String> metadata, AccessTier tier, BlobRequestConditions requestConditions, Duration timeout, Context context)而不是您当前使用的upload方法。

You will be able to define content type using BlobHttpHeaders .您将能够使用BlobHttpHeaders定义内容类型。

Faced the same issue uploading JSON file, came up with this from stepping through the blobClient.upload method you're current using:在上传 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);

Note this is using azure-storage-blob v12.19.0请注意,这是使用 azure-storage-blob v12.19.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM