gpt4 book ai didi

c# - 从 Azure 应用服务访问 Blob 存储

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:57 28 4
gpt4 key购买 nike

我在从应用服务移动应用(不是 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'; Secondary = 'http://127.0.0.1:10000/devstoreaccount1-secondary' http://127.0.0.1:10000/devstoreaccount1 temp storage :

这表明对存储模拟器的访问是不需要的。

问题

如何获取 Azure 在线存储Uri,例如从 Azure 应用服务访问它。

根据评论更新

我将对云配置的请求解释为 Azure 门户上应用服务 的应用程序设置。

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

最佳答案

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

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

2)我将您的代码复制粘贴到 HomeController Index 操作中,并能够重新创建问题。基本上,看起来您更改了值和变量。工作代码如下。第一个片段用于 Controller ,第二个片段应位于索引 View 中。我使用了 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();

某处的索引 View :

@ViewBag.LocalStorage
@ViewBag.CloudStorage

enter image description here

关于c# - 从 Azure 应用服务访问 Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925601/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com