【问题标题】:cannot find uploaded image when using Azure Storage Emulator in local machine在本地计算机上使用 Azure 存储模拟器时找不到上传的图像
【发布时间】:2014-06-05 06:16:54
【问题描述】:

asp.net 4.5、Web 表单、vs2013、Identity 2.0、Entity Framework 6.0

我计划使用 Azure 存储 blob 来存储用户上传的图像。所以我下载了 Azure Storage Emulator 在我的本地机器上进行测试。

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

但问题是我在我的网页上看不到这张图片。调试代码,我看到这张图片的URI是

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

我将此网址粘贴到浏览器,IE 告诉我:

The webpage cannot be found 404

Chrome 告诉我:

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>

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

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;
    }
}

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

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

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

【问题讨论】:

    标签: asp.net azure-blob-storage


    【解决方案1】:

    这是因为容器默认关闭公共读取访问。如果您使用Visual Studio Server ExplorerPublic Read Access 属性更改为Container,您将能够从浏览器访问该blob

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

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

    【讨论】:

    • 谢谢!我有 2 个后续问题:(1)我在代码中创建容器。您似乎通过拖放创建容器,您是如何做到的? (2) 我可以在我的测试中将权限设置为公开,但是当我将它部署到 Azure 时,我如何保证这些图像只能由授权用户访问?
    • 1) 我正在使用服务器资源管理器执行此操作。 msdn.microsoft.com/en-us/library/x603htbk.aspx.
    • 2) 是否只能在使用开发存储时才设置权限为BlobContainerPublicAccessType.Container?默认情况下,权限设置为 BlobContainerPublicAccessType.Off。
    • 嘿,由于VS 2019的问题,您知道如何在“服务器资源管理器”中打开VS 2019中的模拟存储。我不明白..我只能在云资源管理器中打开它,并且没有更改“公共读取访问权限”的选项....
    【解决方案2】:

    另一种方法是直接通过 Microsoft Azure Storage Explorer。 右键单击您的 blob 容器并选择设置公共访问级别...

    Microsoft Azure Storage Explorer

    【讨论】:

      【解决方案3】:

      在创建集公共访问权限时:

      public async ValueTask<bool> CreateContainer(string name)
      {
          try {
              await _blobServiceClient.CreateBlobContainerAsync(name, PublicAccessType.BlobContainer);
              return true;
          }
          catch { }
          return false;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-11
        • 2021-08-24
        • 2017-06-26
        • 1970-01-01
        • 2019-05-19
        • 2021-08-26
        • 1970-01-01
        • 2015-08-21
        • 2021-03-21
        相关资源
        最近更新 更多