简体   繁体   中英

Blob storage access from Azure App Service

I have an issue with accessing blob storage from an App Service Mobile App (Not MobileService). I previously have had a MobileService running that accessed the Blob Storage in the following way:

// Set the URI for the Blob Storage service.
Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", storageAccountName));

// Create the BLOB service client.
CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint,
new StorageCredentials(storageAccountName, storageAccountKey));

Updating the code to work on the new service, still did not help. The dataconnection seems correct:

Therefore referring to these links azure configuration | azure connection string | azure get started blob storage .

I have extracted the data connection and have implemented the `MS_AzureStorageAccountConnectionString. I have the following methods to verify the correct access is found:

string tempstorage = "";
try
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("MS_AzureStorageAccountConnectionString"));
    tempstorage = storageAccount.BlobEndpoint.ToString() + "        " + storageAccount.BlobStorageUri.ToString();

    //Uri blobEndpoint = storageAccount.TableStorageUri.GetUri(StorageLocation.Primary);
}
catch
{
}
string cloud = "";
try
{
    CloudStorageAccount temp = CloudStorageAccount.DevelopmentStorageAccount;
    Uri endPoit = temp.BlobEndpoint;
    string uri = temp.BlobStorageUri.ToString();
    cloud = uri + "           " + endPoit.ToString();
 }
 catch
 {
 }
 return Ok("coud : " + cloud + "       temp storage : " + tempstorage);

return value:

coud : Primary = ' http://127.0.0.1:10000/devstoreaccount1 '; Secondary = ' http://127.0.0.1:10000/devstoreaccount1-secondary ' http://127.0.0.1:10000/devstoreaccount1 temp storage :

This shows the access is to the Storage emulator which is not desired.

Question

How to get the Uri for the Azure online storage such as to access it from the Azure app service .

Update based on the comment

I interpreted the request for cloud configuration as the application settings for the App Service on the azure portal.

<configuration>
     <connectionStrings>
         <add name="MS_AzureStorageAccountConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=Name;AccountKey=key1_from_access_Keys" />
     </connectionStrings>
<configuration>

I tried to re-create the issue using your code, and that is what i done:

1) Click on the project => Add => Add Connected Service => Azure Storage => Select your storage account. That will install all of needed libraries and valid connection string into your web.config.

2) I copy-pasted your code into the HomeController Index action and was able to re-create the issue. Basically, looks like you changed the values and variables. The working code is below. The first snippet is for Controller, the second should be in the Index view. I used MVC, and you looks like using Web API, should not make any difference.

string tempstorage = "";
        string cloud = "";
        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("allinazure_AzureStorageConnectionString"));
            cloud = storageAccount.BlobEndpoint.ToString() + "        " + storageAccount.BlobStorageUri.ToString();                
        }
        catch
        {
        }

        try
        {
            CloudStorageAccount temp = CloudStorageAccount.DevelopmentStorageAccount;
            Uri endPoit = temp.BlobEndpoint;
            string uri = temp.BlobStorageUri.ToString();
            tempstorage = uri + "           " + endPoit.ToString();
        }
        catch
        {
        }
     ViewBag.LocalStorage = "cloud storage" + cloud;
        ViewBag.CloudStorage = "local storage : " + tempstorage;

        return View();

Index view somewhere:

@ViewBag.LocalStorage
@ViewBag.CloudStorage

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM