从 Azure 应用服务访问 Blob 存储
Blob storage access from Azure App Service
我在从应用服务移动应用程序(非移动服务)访问 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);
return 值:
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 :
这表明正在访问不需要的 Storage emulator
。
问题
如何为 Azure online storage
获取 Uri
,例如从 Azure app service
访问它。
根据评论更新
我将云配置请求解释为 Azure 门户上 App Service
的应用程序设置。
<configuration>
<connectionStrings>
<add name="MS_AzureStorageAccountConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=Name;AccountKey=key1_from_access_Keys" />
</connectionStrings>
<configuration>
我尝试使用您的代码重新创建问题,这就是我所做的:
1) 单击项目 => 添加 => 添加连接服务 => Azure 存储 => Select 您的存储帐户。这会将所有需要的库和有效的连接字符串安装到您的 web.config.
2) 我将您的代码复制粘贴到 HomeController Index 操作中,并且能够重新创建问题。基本上,看起来您更改了值和变量。
工作代码如下。第一个片段用于 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
我在从应用服务移动应用程序(非移动服务)访问 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);
return 值:
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 :
这表明正在访问不需要的 Storage emulator
。
问题
如何为 Azure online storage
获取 Uri
,例如从 Azure app service
访问它。
根据评论更新
我将云配置请求解释为 Azure 门户上 App Service
的应用程序设置。
<configuration>
<connectionStrings>
<add name="MS_AzureStorageAccountConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=Name;AccountKey=key1_from_access_Keys" />
</connectionStrings>
<configuration>
我尝试使用您的代码重新创建问题,这就是我所做的:
1) 单击项目 => 添加 => 添加连接服务 => Azure 存储 => Select 您的存储帐户。这会将所有需要的库和有效的连接字符串安装到您的 web.config.
2) 我将您的代码复制粘贴到 HomeController Index 操作中,并且能够重新创建问题。基本上,看起来您更改了值和变量。 工作代码如下。第一个片段用于 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