CloudStorageAccount与StorageCredentials的SAS令牌

时间:2014-07-21 14:26:09

标签: c# azure-storage-blobs

Following this Microsoft guide

我尝试使用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);
}

使用凭据的代码:

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构造函数没有重载。

我感谢任何帮助。

汤姆

3 个答案:

答案 0 :(得分:1)

当您知道帐户名称和端点后缀时,可以使用Uri和凭据创建Client对象。您实际上不需要创建云存储帐户。具体来说,可以使用此客户端构造函数: CloudBlobClient(URI / * http://account.blob.core.windows.net * /,creds);

获得客户端对象后,可以先在客户端上使用GetContainerReference方法继续创建容器,然后在容器本身上调用CreateIfNotExists方法。

答案 1 :(得分:1)

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, "account-name", endpointSuffix: null, useHttps: true);

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

答案 2 :(得分:0)

com.microsoft.azure.azure-storage使用最新的maven依赖项

<dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>8.4.0</version> 
</dependency>