0
votes

PROBLEM

I am using the code below to generate a SAS URI for a Azure Storage Account which works a treat.

However when executing the code the resulting console application window displays the relevant URI string but I am unable to copy the value.

QUESTION

Easy question hopefully , what has to be added to the code below to output the URI to a text file ?

{

using System.IO;

using Microsoft.WindowsAzure;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; using System.Threading.Tasks;

namespace SAS

{ class Program { static void Main(string[] args) {

//Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

//Get a reference to a container to use for the sample code, and create it if it does > not exist.
CloudBlobContainer container = blobClient.GetContainerReference("sascontainer");
container.CreateIfNotExists();

//Insert calls to the methods created below here...


//Generate a SAS URI for the container, without a stored access policy.
Console.WriteLine("Container SAS URI: " + GetContainerSasUri(container));
Console.WriteLine();

//Require user input before closing the console window.
Console.ReadLine();
    }



    static string GetContainerSasUri(CloudBlobContainer container)
    {
        //Set the expiry time and permissions for the container.
        //In this case no start time is specified, so the shared access signature > becomes valid immediately.
        SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
        sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(4);
        sasConstraints.Permissions = SharedAccessBlobPermissions.Write | > SharedAccessBlobPermissions.List;

        //Generate the shared access signature on the container, setting the > constraints directly on the signature.
        string sasContainerToken = > container.GetSharedAccessSignature(sasConstraints);

        //Return the URI string for the container, including the SAS token.
        return container.Uri + sasContainerToken;
    }

}

} }

2

2 Answers

2
votes

If you just want to get the SAS string from console, Powershell will be a quicker approach.

Firstly ensure you installed Azure PowerShell, and authenticate your Azure PowerShell session by either Add-AzureAccount or Import-AzurePublicSettingsFile

Then, you have created your storage account with account_name, and a container with container_name:

$context = New-AzureStorageContext -StorageAccountName account_name -StorageAccountKey (Get-AzureStorageKey -StorageAccountName account_name).Primary
$container = (Get-AzureStorageContainer -Name container_name -Context $context).CloudBlobContainer
$sp = New-Object -TypeName Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy
$sp.SharedAccessExpiryTime = [System.DateTime]::Now.AddHours(4)
$sp.Permissions = ([Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions]::Write -bor [Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions]::List)
$uriwithsas = $container.Uri.AbsoluteUri + $container.GetSharedAccessSignature($sp)
echo $uriwithsas
1
votes

Not sure if this is what you're looking for, but the answer is rather simple. To save the output to a file, can't you just do:

            File.WriteAllText(@"C:\output.txt", "Container SAS URI: " + GetContainerSasUri(container))

If you want to copy the contents from command prompt, here's what you would need to do:

  1. Right click on the command prompt.
  2. Click Mark.
  3. Drag the cursor to select the text.
  4. Hit Enter key to copy the selected text to clipboard.

Or ... am I completely misunderstanding your question?