我有一个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;
}
修改
进一步阅读后我改变了
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
return blockBlob;
到
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.SetProperties();
return blockBlob;
此更改会返回异常
异常详细信息:System.Net.WebException:远程服务器返回错误:(404)Not Found。
上传时我是否需要做一些不同的事情?
修改
@Gaurav Mantri
我可以看到它存储为application/octet-stream
,但是当我尝试使用
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.SetProperties();
我在set属性上遇到了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);
}
}
答案 0 :(得分:5)
感谢Gaurav的评论,我能够解决我的问题。
我正在下载文件
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.SetProperties();
return blockBlob;
但是在存储文件时我没有设置内容类型,所以它被保存为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);
}