gpt4 book ai didi

c# - 具有 SAS token 的 StorageCredentials 的 CloudStorageAccount

转载 作者:行者123 更新时间:2023-11-30 16:11:03 26 4
gpt4 key购买 nike

Following this Microsoft guide

我尝试使用 sasToken 凭据获取对容器的引用。

我创建了一个 sas token ,然后创建了凭据:(更改了 sas token 中的一些字母...)

public static StorageCredentials GetContainerCredentials()
{
string sasToken = "?sv=2014-02-14&sr=c&si=read%20only%20policy&sig=JpCYrvZPXuVqlflu6BOZMh2MxfghoJt8GMDyVY7HOkk%3D";
return new StorageCredentials(sasToken);
}

使用凭据的代码:

public bool Init(string ContainerName, StorageCredentials credentials)
{
try
{
m_containerName = ContainerName;
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);

if (null == storageAccount)
{
Console.WriteLine("storageAccount is null");
return false;
}

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

if (null == blobClient)
{
Console.WriteLine("blobClient is null");
return false;
}

// Retrieve a reference to a container.
m_container = blobClient.GetContainerReference(ContainerName);

Console.WriteLine("Init success");

return true;
}
catch (Exception ex)
{
Console.WriteLine("Azure init exception: " + ex.Message);
}
m_container = null;
return false;
}

运行代码时出现异常:

CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);  

异常:

System.ArgumentNullException: Value cannot be null.
Parameter name: accountName

我发现 StorageCredentials 构造函数没有重载,它接受 sasToken 和帐户名称。

感谢任何帮助。

汤姆

最佳答案

static void UseAccountSAS(string sasToken)
{
// Create new storage credentials using the SAS token.
StorageCredentials accountSAS = new StorageCredentials(sasToken);

// Use these credentials and the account name to create a Blob service client.
CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, "accountname", endpointSuffix: null, useHttps: true);

注意:“accountname”是您的 Azure 存储帐户的名称,如端点“https://accountname.blob.core.windows.net”中所示。

来源:https://learn.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1?toc=%2fazure%2fstorage%2fblobs%2ftoc.json

关于c# - 具有 SAS token 的 StorageCredentials 的 CloudStorageAccount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867582/

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