在本地计算机上使用Azure存储模拟器时找不到上载的图像 [英] cannot find uploaded image when using Azure Storage Emulator in local machine

查看:75
本文介绍了在本地计算机上使用Azure存储模拟器时找不到上载的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asp.net 4.5,Web窗体,vs2013,Identity 2.0,实体框架6.0

我计划使用Azure存储blob存储用户上传的图像.因此,我下载了Azure存储仿真器以在本地计算机上进行测试.

似乎容器已正确创建,并且图像已正确保存-因为运行应用程序时没有收到任何异常.

但是问题是我无法在网页上看到此图像.调试代码,我看到该图像的URI为

  http://127.0.0.1:10000/devstoreaccount1/images/my_sub_folder_2/my_image.jpg 

我将这个URL粘贴到浏览器中,IE告诉我:

 找不到网页404 

Chrome告诉我:

 此XML文件似乎没有与之关联的任何样式信息.文档树如下所示.<错误>< Code> ResourceNotFound</Code><消息>指定的资源不存在.RequestId:e4b64fa8-5985-4aa4-bfbe-50aabd497537时间:2014-04-21T07:26:04.4264011Z</消息></错误> 

我使用AzureStorageAccess.CreateContainer()在存储中创建一个容器,并使用AzureStorageAccess.UploadBlob()将用户上传的图像(流)保存到存储中.代码如下.

 公共类AzureStorageAccess{//在Azure Storage Emulator中创建一个容器静态公共无效CreateContainer(string containerName){CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage = true");//CloudConfigurationManager.GetSetting("StorageConnectionString")CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();CloudBlobContainer容器= blobClient.GetContainerReference(containerName);container.CreateIfNotExists();}//将流上传到Azure存储模拟器静态公共无效UploadBlob(System.IO.Stream流,字符串containerName,字符串blobRelativeURI,输出字符串blobURI){CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage = true");//CloudConfigurationManager.GetSetting("StorageConnectionString")CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();CloudBlobContainer容器= blobClient.GetContainerReference(containerName);CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobRelativeURI);blockBlob.UploadFromStream(stream);blobURI = blockBlob.Uri.AbsoluteUri;}} 

我在web.config中有此连接字符串.最初,我使用此连接字符串.稍后,当我尝试解决问题时,我在stackoverflow上阅读了一些我应该使用 Parse("UseDevelopmentStorage = true")的帖子.您可以在上面的代码中注释掉读取此连接字符串的代码.

 <代码><添加键= StorageConnectionString" 值= DefaultEndpointsProtocol = HTTP;帐户名= devstoreaccount1; AccountKey = Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw ==; BlobEndpoint = HTTP://127.0.0.1:10000/devstoreaccount1"/> 

谁能告诉我这是什么问题?非常感谢你!

解决方案

之所以会这样,是因为默认情况下容器会关闭公共读取访问权限.如果使用 Visual Studio Server Explorer 公共读取访问属性更改为容器,则可以从浏览器访问blob.>

或者,如果您想通过SDK在代码中执行此操作,请在创建容器之前添加以下行.

 //创建容器container.SetPermissions(new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Container});container.CreateIfNotExists(); 

asp.net 4.5, Web Forms, vs2013, Identity 2.0, Entity Framework 6.0

I plan to use Azure Storage blob to store user uploaded images. So I downloaded Azure Storage Emulator to test in my local machine.

It seems container is created correctly and image is saved correctly - since I didn't receive any exception when I run the application.

But the problem is that I cannot see this image on my web page. Debugging into the code, I saw this image's URI is

http://127.0.0.1:10000/devstoreaccount1/images/my_sub_folder_2/my_image.jpg

I paste this url to browser, IE tells me:

The webpage cannot be found 404

Chrome tells me:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>ResourceNotFound</Code>
<Message>
The specified resource does not exist. RequestId:e4b64fa8-5985-4aa4-bfbe-50aabd497537 Time:2014-04-21T07:26:04.4264011Z
</Message>
</Error>

I use AzureStorageAccess.CreateContainer() to create a container in the storage, and use AzureStorageAccess.UploadBlob() to save user uploaded image (stream) to the storage. The code is as follows.

public class AzureStorageAccess
{
// create a container in Azure Storage Emulator
    static public void CreateContainer(string containerName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount
            .Parse("UseDevelopmentStorage=true");
        // CloudConfigurationManager.GetSetting("StorageConnectionString")
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();
    }

// upload a stream to Azure Storage Emulator
    static public void UploadBlob(System.IO.Stream stream, string containerName, string blobRelativeURI, out string blobURI)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount
            .Parse("UseDevelopmentStorage=true");
        // CloudConfigurationManager.GetSetting("StorageConnectionString")
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobRelativeURI);
        blockBlob.UploadFromStream(stream);
        blobURI = blockBlob.Uri.AbsoluteUri;
    }
}

I have this connection string in web.config. Initially I was using this connection string. Later when I am trying to solve the problem, I read some posts here in stackoverflow that I should use Parse("UseDevelopmentStorage=true"). You can see the code to read this connection string is commented out in above code.

<add key="StorageConnectionString"         value="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1"
/>  

Can anyone tell me what is the problem? Thank you very much!

解决方案

That is happening because containers by default turn off public read access. You will be able to access the blob from the browser if you change the Public Read Access property to Container using the Visual Studio Server Explorer

Or if you want to do this in code via the SDK add the following line before you create the container.

        //Create container
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
        container.CreateIfNotExists();

这篇关于在本地计算机上使用Azure存储模拟器时找不到上载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆