我有一个Asp Mvc应用程序,我试图显示存储在Azure Blob存储中的图像。当我返回字符串时,浏览器只显示为破损的图像。当我右键单击并“检查”并单击生成的URI时,我会收到以下消息:“BlobNotFound – 指定的Blob不存在。RequestId:f7c32876-0001-013e-539d-dede28000000 Time:2016-07-15T13:30:49.7560775Z”。我已下载Azure Storage Explorer并可以查看文件。我尝试将容器上的访问级别从“无公共访问”更改为“仅读取Blob的访问权限”,但我认为我已在代码中使用该行“PublicAccess = BlobContainerPublicAccessType.Blob”进行了更改。如何显示存储中包含的图像?
我在设置属性时出现了404异常。当我拉取图片时,调试时可以看到图片名称与上传时的名称相同。
上传时我使用了以下代码:
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;
to
// 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) 未找到。
我需要在上传时做些不同的事情吗?
编辑
@Gaurav Mantri
我看到它存储为application/octet-stream
,但当我尝试将其更改为image/jpg时使用:
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.SetProperties();
我在设置属性时出现了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);
}
}
PS-ICON-120x120_140.jpg
的文件了吗?你能否通过Azure Storage Explorer分享一下它的截图? - Gaurav MantriUploadFile
中,您没有像在GetBlobInContainer
函数中那样设置blob容器的ACL。您能否请检查一下blob容器的ACL确实是“Blob”而不是“Private”?2)实际上,您向GetBlobInContainer
函数传递的blob名称与您在UploadFile
中所做的不同。请确保两者相同。 - Gaurav Mantri