Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 974
Tom Avatar asked Jul 21 '14 14:07

Tom


People also ask

How do I know if my SAS token is valid?

The first is to build it into the SAS token itself. Then the only way to check expiry is to inspect the se= parameter of the token. You could maintain a list of known SAS tokens and alert based on the expiry. The second way to set expiry is to set it in a stored policy on a container.

What is SAS token authentication?

SAS Token Authentication. Summary. The metadata server generates and validates a single-use identity token for each authentication event. This has the effect of causing participating SAS servers to accept users who are connected to the metadata server.


2 Answers

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://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1?toc=%2fazure%2fstorage%2fblobs%2ftoc.json

like image 80
SkyCaptain Avatar answered Sep 23 '22 14:09

SkyCaptain


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.

like image 38
Veena Udayabhanu - MSFT Avatar answered Sep 25 '22 14:09

Veena Udayabhanu - MSFT