我正在尝试访问Blob存储帐户,并仅使用SAS令牌列出容器的内容。我正在生成访问策略(根据此doc),并在我的访问令牌中引用该访问策略。
不幸的是,当我的代码尝试运行ListBlobsSegmented
函数时,我收到403(禁止)错误。
这是尝试获取blob列表的代码:
string storageAccountName = "ringclone";
string containerName = "ringcentral-archives";
string authenticationKey = "?sv=2018-03-28&si=ringclone-access-policy&sr=c&sig=************************";
StorageCredentials creds;
CloudStorageAccount account;
CloudBlobClient blobClient;
CloudBlobContainer cloudBlobContainer;
creds = new StorageCredentials(authenticationKey);
account = new CloudStorageAccount(creds, storageAccountName, endpointSuffix: null, useHttps: true);
blobClient = account.CreateCloudBlobClient();
cloudBlobContainer = blobClient.GetContainerReference(containerName);
BlobContinuationToken blobContinuationToken = null;
var containerSegment = blobClient.ListBlobsSegmented("", blobContinuationToken); // 403 error;
这些是我用来生成访问策略并生成引用该访问策略的SAS令牌的步骤:
查询字符串字段用作身份验证密钥
string storageAccountName = "ringclone";
string containerName = "ringcentral-archives";
string authenticationKey = "?sv=2018-03-28&si=ringclone-access-policy&sr=c&sig=************************"; // retrieved from the "Query String" field in storage explorer.
但是,尝试列出容器中的Blob时出现403错误。我在做什么错了?
答案 0 :(得分:1)
在控制台应用中尝试以下代码:
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
using System;
namespace AzureStorageTest
{
class Program
{
static void Main(string[] args)
{
string storageAccountName = "<storage account name>";
string containerName = "<container name>";
string sasToken = "<sas token>";
StorageCredentials creds;
CloudBlobContainer cloudBlobContainer;
creds = new StorageCredentials(sasToken);
cloudBlobContainer = new CloudBlobContainer(new Uri("https://"+ storageAccountName + ".blob.core.windows.net/"+ containerName), creds);
BlobContinuationToken blobContinuationToken = null;
var blobs = cloudBlobContainer.ListBlobsSegmented("", blobContinuationToken);
foreach (var blob in blobs.Results) {
Console.WriteLine(blob.Uri);
}
Console.ReadKey();
}
}
}
结果:
希望对您有帮助