显示来自Azure Blob容器的图像

ton*_*9uk 6 c# azure azure-storage azure-web-sites

我有一个ASP MVC应用程序,试图在其中显示已存储在Azure Blob存储中的图像。

当我返回字符串时,浏览器只是显示为损坏的图像。当我右键单击并“检查”并单击生成的URI时,我收到以下消息

BlobNotFound 指定的Blob不存在。RequestId:f7c32876-0001-013e-539d-dede28000000时间:2016-07-15T13:30:49.7560775Z

我已经下载Azure Storage Explorer并可以看到文件。我试图将容器上的访问级别更改为read access for blobs only从no public access(但是我认为我在代码行中这样做了PublicAccess = BlobContainerPublicAccessType.Blob)。

如何显示存储中包含的图像?

public string Index()
    {
        CloudBlockBlob blob = GetBlobInContainer("blobContainerTjsContainer", "PS-ICON-120x120_140.jpg");
        return "<img src="+ blob.Uri.AbsoluteUri +" alt='PS Image'>";
    }

CloudBlockBlob GetBlobInContainer(string container, string fileName)
    {
        //use web.config appSetting to get connection setting .NET Framework's ConfigurationManager class can also be used for this
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        //create the blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        //retrieve a refernce to a container
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(CloudConfigurationManager.GetSetting(container));
        //set permission to show to public
        blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob});
        blobContainer.CreateIfNotExists();
        // Retrieve reference to a blob named "photo1.jpg".
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
        return blockBlob;
    }
Run Code Online (Sandbox Code Playgroud)

编辑

进一步阅读后,我改变了

// Retrieve reference to a blob named "photo1.jpg".
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
        return blockBlob;
Run Code Online (Sandbox Code Playgroud)

至

// Retrieve reference to a blob named "photo1.jpg".
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
        blockBlob.Properties.ContentType = "image/jpg";
        blockBlob.SetProperties();
        return blockBlob;
Run Code Online (Sandbox Code Playgroud)

此更改返回异常

异常详细信息:System.Net.WebException:远程服务器返回错误:(404)找不到。

上传时我需要做些不同的事情吗?

编辑

@Gaurav Mantri

天蓝色存储

我可以看到它存储为,application/octet-stream但是当我尝试将其更改为image / jpg时

blockBlob.Properties.ContentType = "image/jpg";
    blockBlob.SetProperties();
Run Code Online (Sandbox Code Playgroud)

我在设置属性时遇到404异常。当我拉图像时,在调试时我可以看到图像名称与上载时的名称相同

在上传时,我使用了以下代码

    void UploadFile(string filePath)
    {
        //use web.config appSetting to get connection setting .NET Framework's ConfigurationManager class can also be used for this
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        //create the blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        //retrieve a refernce to a container
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(CloudConfigurationManager.GetSetting("blobContainerTjsContainer"));
        //create a container if it doesnt exist
        blobContainer.CreateIfNotExists();
        //gets the reference to the blob that will be written or OVER written
        CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference("my-image-blob");

        using (var fileStream = System.IO.File.OpenRead(filePath))
        {
            blockBlob.UploadFromStream(fileStream);
        }
    }
Run Code Online (Sandbox Code Playgroud)

ton*_*9uk 7

感谢Gaurav的评论,我得以解决我的问题。

我正在按以下方式下载文件

CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.SetProperties();
return blockBlob;
Run Code Online (Sandbox Code Playgroud)

但是在存储文件时,我没有设置内容类型,因此将其另存为application/octet-stream,上面的代码试图在调用文件时转换类型。通过在上传之前设置类型,我可以简单地调用文件并将其显示为图像,例如

CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference("my-image-blob");
blockBlob.Properties.ContentType = "image/jpg";

using (var fileStream = System.IO.File.OpenRead(filePath))
{
    blockBlob.UploadFromStream(fileStream);
}
Run Code Online (Sandbox Code Playgroud)