Tom
Tom

Reputation: 71

CloudStorageAccount with StorageCredentials of SAS token

Following this Microsoft guide

I try to get reference to container with sasToken credentials.

I created a sas token and then created credentials: (changed some letters here in the 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);
}

The code which uses the credentials:

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;
}

when running the code I get exception on the line:

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

the exception:

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

I found no overload to StorageCredentials constructor which accepts sasToken and Account Name.

I appreciate any help.

Tom

Upvotes: 7

Views: 13539

Answers (3)

SkyCaptain
SkyCaptain

Reputation: 107

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);

Note: "accountname" is the name of your Azure Storage Account as in the endpoint "https://accountname.blob.core.windows.net".

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

Upvotes: 8

Ganesh
Ganesh

Reputation: 757

Use latest maven dependency for com.microsoft.azure.azure-storage

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

Upvotes: -1

Veena Udayabhanu - MSFT
Veena Udayabhanu - MSFT

Reputation: 1269

When you know the account name and the endpoint suffix, you can create a Client object using the Uri and credentials. You don't actually don’t need to create the Cloud storage account. Specifically, this client constructor can be used: CloudBlobClient(URI /* http://account.blob.core.windows.net */, creds);

Once you have the client object, you can proceed to creating the container by using the GetContainerReference method on the client first and then calling CreateIfNotExists method on the container itself.

Upvotes: 2

Related Questions