【问题标题】:Blob storage access from Azure App Service从 Azure 应用服务访问 Blob 存储
【发布时间】:2016-08-23 20:42:53
【问题描述】:

我在从应用服务移动应用(非 MobileService)访问 Blob 存储时遇到问题。我之前运行过一个 MobileService,它通过以下方式访问 Blob 存储:

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

更新代码以使用新服务,但仍然没有帮助。数据连接似乎正确:

因此参考这些链接azure configuration | azure connection string | azure get started blob storage

我已经提取了数据连接并实现了`MS_AzureStorageAccountConnectionString。我有以下方法来验证是否找到了正确的访问权限:

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

返回值:

coud : Primary = 'http://127.0.0.1:10000/devstoreaccount1';次要 = 'http://127.0.0.1:10000/devstoreaccount1-secondary' http://127.0.0.1:10000/devstoreaccount1 临时存储:

这表明对Storage emulator 的访问是不希望的。

问题

如何获取Azure online storageUri,例如从Azure app service 访问它。

根据评论更新

我将云配置请求解释为 azure 门户上 App Service 的应用程序设置。

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

【问题讨论】:

  • 抱歉,不是很清楚。您能否澄清一下,您收到了什么,您需要收到什么? URI 似乎是正确的。
  • @AlexBelotserkovskiy 好的,我会在使用电脑时重写。我的问题是 URI 方案与 Mobileservice 完全不同。为什么是这样?同样,uri 有 devstoreaccount1 不是存储的名称? URI是因为它们存在于同一个资源组中,因此具有本地连接吗? (我可以看到我写的问题太久了。我会重写,谢谢你引起我的注意!)
  • Devstoreaccount 是本地存储模拟器。 127.0.0.1 也是如此。检查您的云配置中的内容 - 如果您看到 devstirageaccount 这意味着您的项目以本地为目标
  • 谢谢!不用担心 - 您提供的信息对故障排除也很有帮助。
  • 您可能对一项新功能感兴趣,该功能将为您为客户端生成 SAS 令牌,并且手动代码要少得多。它还适用于离线同步,因此您可以将 blob 上传/下载排队到您有活动连接时。见Connect to Azure Storage in your Xamarin.Forms app

标签: c# azure azure-storage azure-web-app-service azure-blob-storage


【解决方案1】:

我尝试使用您的代码重新创建问题,这就是我所做的:

1) 单击项目 => 添加 => 添加连接服务 => Azure 存储 => 选择您的存储帐户。这会将所有需要的库和有效的连接字符串安装到您的 web.config 中。

2) 我将您的代码复制粘贴到 HomeController Index 操作中,并且能够重新创建问题。基本上,看起来您更改了值和变量。 工作代码如下。第一个 sn-p 用于 Controller,第二个应该在 Index 视图中。我用的是MVC,你好像用的是Web API,应该没什么区别。

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

索引视图某处:

@ViewBag.LocalStorage
@ViewBag.CloudStorage

【讨论】:

  • 非常感谢您的帮助,我有一个关于为两个客户提供一项服务的特色问题(奖金)stackoverflow.com/questions/36402899/…。再次感谢您的成功:)
  • 在这个测试之后它似乎工作了,现在我已经更新了客户端并创建了一个新服务,上面的代码工作正常。但我以来自以下await container.CreateIfNotExistsAsync(); 函数的错误结束。如果您有时间,我会再次感谢您的帮助stackoverflow.com/questions/37050344/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 2020-06-03
  • 2021-12-18
  • 2012-09-30
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多