Azure存储不显示作为应用程序/八位字节流或不带内容类型上传的文件

uqzxnwby  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(97)

我有以下代码要上传到Azure存储:

BlobClient blob = container.GetBlobClient(guid);
            var blobHttpHeaders = new BlobHttpHeaders();
            if (contentType != null)
            {
                blobHttpHeaders.ContentType = contentType;
            }
            var response = blob.Upload(new MemoryStream(bytes), blobHttpHeaders);

如果我将contentType设置为image/png,那么我可以在Azure Portal中或通过blob链接看到该图像。
但是,当我想上传PDF或SVG文件时,我将contentType设置为application/octet-stream,然后我看到代码201和Created响应,但我无法在Azure Portal中看到文件,当我使用blob链接时,我得到404。
这是怎么回事?

4dc9hkyq

4dc9hkyq1#

我尝试了下面的代码,将一个PDF文件上传到一个存储帐户,contentType设置为application/octet-stream,并成功上传了一个blob作为PDF文件。

验证码:-

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string connectionString = "<connec_string>";
        string containerName = "<container_name>";
        string blobName = "<blob_name>";
        byte[] bytes = File.ReadAllBytes("<your_file>.pdf"); 

        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        var blobHttpHeaders = new BlobHttpHeaders
        {
            ContentType = "application/octet-stream" 
        };

        using (var stream = new MemoryStream(bytes))
        {
            blobClient.Upload(stream, blobHttpHeaders);
        }

        Console.WriteLine("Blob uploaded successfully.");
    }
}

输出

  • blob上传成功,如下所示:*

  • blob被上传到Azure门户中的存储帐户容器,我点击下载以检查数据,如下所示:-*

  • 下载的blob如下所示:*

相关问题