Tom*_*Tom 7 c# azure-storage-blobs
我尝试使用sasToken凭据获取对容器的引用。
我创建了一个 sas 令牌,然后创建了凭据:(在 sas 令牌中更改了一些字母...)
public static StorageCredentials GetContainerCredentials()
{
string sasToken = "?sv=2014-02-14&sr=c&si=read%20only%20policy&sig=JpCYrvZPXuVqlflu6BOZMh2MxfghoJt8GMDyVY7HOkk%3D";
return new StorageCredentials(sasToken);
}
Run Code Online (Sandbox Code Playgroud)
使用凭证的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
运行代码时,我在线上遇到异常:
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);
Run Code Online (Sandbox Code Playgroud)
例外:
System.ArgumentNullException: Value cannot be null.
Parameter name: accountName
Run Code Online (Sandbox Code Playgroud)
我发现StorageCredentials接受sasToken和帐户名称的构造函数没有重载。
我很感激任何帮助。
汤姆
小智 6
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);
Run Code Online (Sandbox Code Playgroud)
注意:“帐户名”是 Azure 存储帐户的名称,如端点“https://accountname.blob.core.windows.net”中所示。
来源:https : //docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1?toc=%2fazure%2fstorage%2fblobs%2ftoc.json
小智 2
当您知道帐户名称和端点后缀时,您可以使用 Uri 和凭据创建客户端对象。您实际上不需要创建云存储帐户。具体来说,可以使用此客户端构造函数:\nCloudBlobClient(URI /* http://account.blob.core.windows.net */, creds);
\n\n获得客户端对象后,您可以首先在客户端上使用 GetContainerReference 方法,然后在容器本身上调用 CreateIfNotExists 方法来创建容器。
\n| 归档时间: |
|
| 查看次数: |
10090 次 |
| 最近记录: |