locked
The remote server returned an error: (403) Forbidden on container.CreateIfNotExistsAsync() method RRS feed

  • Question

  • static class StorageUtils
        {
            public static CloudStorageAccount StorageAccount
            {
                get
                {
                    string account = ConfigurationManager.AppSettings["StorageAccountName"];
                    //string account = CloudConfigurationManager.GetSetting("StorageAccountName");

                    //string key = CloudConfigurationManager.GetSetting("StorageAccountAccessKey");
                    string key = ConfigurationManager.AppSettings["StorageAccountName"];
                    string connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
                    return CloudStorageAccount.Parse(connectionString);
                }
            }
        }

    I have a web project and I converted it to "MS Azure Cloud Service Project"

    I make the Azure project default and run CreateAndConfigureAsync() to create the container. It works fine and creates the images container in Azure storage.

    Then I make my web project default and change CloudConfigurationManager.GetSetting to ConfigurationManager.AppSettings in StorageUtils class and run the web app project. Then I get "The remote server returned an error: (403) Forbidden" error whencontainer.CreateIfNotExistsAsync() line get executed.

    What is the reason for this?

    async public void CreateAndConfigureAsync()
            {
                try
                {
                    CloudStorageAccount storageAccount = StorageUtils.StorageAccount;

                    // Create a blob client and retrieve reference to images container
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container = blobClient.GetContainerReference("images");

                    // Create the "images" container if it doesn't already exist.
                    if (await container.CreateIfNotExistsAsync())
                    {
                        // Enable public access on the newly created "images" container
                        await container.SetPermissionsAsync(
                            new BlobContainerPermissions
                            {
                                PublicAccess =
                                    BlobContainerPublicAccessType.Blob
                            });
                    }
                }
                catch (Exception ex)
                {            }
            }
    • Edited by DevNC Saturday, October 25, 2014 1:12 AM
    Friday, October 24, 2014 11:15 PM

Answers

  • Hi,

    It seems there is something wrong with your code, from your code the account and key are the same, because their appsetting name are the same.

      string account = ConfigurationManager.AppSettings["StorageAccountName"];
                     //string account = CloudConfigurationManager.GetSetting("StorageAccountName");
    
                     //string key = CloudConfigurationManager.GetSetting("StorageAccountAccessKey");
                     string key = ConfigurationManager.AppSettings["StorageAccountName"];
                     string connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
                     return CloudStorageAccount.Parse(connectionString);

    If you use CloudConfigurationManager.GetSetting, please consider set azure project as the startup project, if not, this value will be null, this resulted in solution being started as a web project that didn't run inside the Azure emulator. Since CloudConfigurationManager.GetSetting tries to get setting by contacting Azure (or Azure emulator in this case), and it is not running, it returns null.

    Best Regards,

    Jambor


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    Monday, October 27, 2014 3:28 AM
  • Hi,

    If you confuse with how to publish azure cloud service, I suggest you have a look at this video: http://channel9.msdn.com/Events/Visual-Studio/Visual-Studio-2012-Virtual-Launch/Developing-Windows-Azure-Cloud-Services-using-Visual-Studio-2012, when we create cloud service with one web role, we will have two project just as you said one web project and one azure project, if we want to publish this cloud service, we need to right click azure project not web project, when we choose azure website(see how to choose azure website, cloud service, virtual machine here: http://azure.microsoft.com/en-us/documentation/articles/choose-web-site-cloud-service-vm/), we only need to publish via right click web project.

    Best Regards,

    Jambor


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 29, 2014 2:04 AM

All replies

  • Hi,

    Please confirm if you are able to access the azure storage account from Visual Studio explorer using the same key used in code. 

    If not, can you login from portal and confirm that your keys have not ben re-generated.

    Regards,

    Manu Rekhar

    Sunday, October 26, 2014 9:34 AM
  • Hi,

    It seems there is something wrong with your code, from your code the account and key are the same, because their appsetting name are the same.

      string account = ConfigurationManager.AppSettings["StorageAccountName"];
                     //string account = CloudConfigurationManager.GetSetting("StorageAccountName");
    
                     //string key = CloudConfigurationManager.GetSetting("StorageAccountAccessKey");
                     string key = ConfigurationManager.AppSettings["StorageAccountName"];
                     string connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
                     return CloudStorageAccount.Parse(connectionString);

    If you use CloudConfigurationManager.GetSetting, please consider set azure project as the startup project, if not, this value will be null, this resulted in solution being started as a web project that didn't run inside the Azure emulator. Since CloudConfigurationManager.GetSetting tries to get setting by contacting Azure (or Azure emulator in this case), and it is not running, it returns null.

    Best Regards,

    Jambor


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    Monday, October 27, 2014 3:28 AM
  • Thanks all, when the Azure project is set as start-up,  it executes CreateIfNotExistsAsync() line without any error and creates the container in my azure data store also.

    Now I have two projects, one is web project and other one is azure project. When I publish the website I have to publish the web project not the azure project, am I correct?

    This is the place I get confused. 
    • Edited by DevNC Tuesday, October 28, 2014 11:42 AM
    Tuesday, October 28, 2014 10:56 AM
  • Hi,

    If you confuse with how to publish azure cloud service, I suggest you have a look at this video: http://channel9.msdn.com/Events/Visual-Studio/Visual-Studio-2012-Virtual-Launch/Developing-Windows-Azure-Cloud-Services-using-Visual-Studio-2012, when we create cloud service with one web role, we will have two project just as you said one web project and one azure project, if we want to publish this cloud service, we need to right click azure project not web project, when we choose azure website(see how to choose azure website, cloud service, virtual machine here: http://azure.microsoft.com/en-us/documentation/articles/choose-web-site-cloud-service-vm/), we only need to publish via right click web project.

    Best Regards,

    Jambor


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 29, 2014 2:04 AM
  • Thank you..
    Monday, November 3, 2014 11:39 PM