Azure Blob存储是否有默认图像类型?

时间:2017-10-20 10:33:45

标签: azure azure-storage-blobs azure-blob-storage

当我将jpg图像上传到Azure Blob Storage时,然后将其拉出来,它总是以png的形式返回。假设默认是安全的吗?我找不到任何文件。我可以轻松地将其转换回我的字节转换中的jpg,因为当我将其拉出容器时存储扩展名,但是我可以安全地假设存储在Azure Blob存储中的任何图像类型都是作为png完成的吗?

2 个答案:

答案 0 :(得分:0)

最好不要假设png是默认值。我认为这取决于您的上传机制以及您在上传过程中为文件指定的名称。

答案 1 :(得分:0)

  

但是我可以安全地假设Azure Blob存储中存储的任何图像类型都是以png

完成的

据我所知,blob存储没有默认的文件类型。它包含一个content-type,它可以告诉用户文件的内容类型是什么。如果设置blob名称是xxx.jpg。然后它会将xxx.jpg存储在blob存储中。

这是一个例子。

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("brandotest.jpg");

        // Create or overwrite the "myblob" blob with contents from a local file.
        using (var fileStream = System.IO.File.OpenRead(@"D:\1.PNG"))
        {
            blockBlob.UploadFromStream(fileStream);
        }

结果:

通过使用存储资源管理器,您可以在blob存储中找到图像的类型。它是brandotest.jpg。

enter image description here

我猜您在使用代码下载时会设置下载图像文件的类型。

像这样(我设置下载文件的路径是myfile.png):

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("brandotest.jpg");

        // Save blob contents to a file.
        using (var fileStream = System.IO.File.OpenWrite(@"D:\authserver\myfile.png"))
        {
            blockBlob.DownloadToStream(fileStream);
        }

结果将是myfile.png。

enter image description here

总而言之,当您使用代码获取或上传文件时,您已下载到本地或上传到blob的文件类型将根据您的设置。