Blob storage access from Azure App Service

て烟熏妆下的殇ゞ 提交于 2019-12-12 20:39:27

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/36925601/blob-storage-access-from-azure-app-service

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