cannot find uploaded image when using Azure Storage Emulator in local machine

核能气质少年 提交于 2021-02-06 10:34:27

问题


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!


回答1:


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

enter image description here

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();



回答2:


Another way of doing it is directly trough Microsoft Azure Storage Explorer. Right click on your blob container and select Set Public Access Level...

Microsoft Azure Storage Explorer



来源:https://stackoverflow.com/questions/23193060/cannot-find-uploaded-image-when-using-azure-storage-emulator-in-local-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!