This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Azure.Storage; | |
using Azure.Storage.Sas; | |
using Azure.Storage.Blobs; | |
using Azure.Storage; | |
using System.Collections.Generic; | |
using System.Web; | |
using Azure.Storage.Files.Shares; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
private string GetKeyValueFromConnectionString(string key) | |
{ | |
var connectionString = "CONNECTIONSTRING"; | |
IDictionary<string, string> settings = new Dictionary<string, string>(); | |
var splitted = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); | |
foreach (var nameValue in splitted) | |
{ | |
var splittedNameValue = nameValue.Split(new char[] { '=' }, 2); | |
settings.Add(splittedNameValue[0], splittedNameValue[1]); | |
} | |
return settings[key]; | |
} | |
public void GetAdHocContainerSasToken() | |
{ | |
var containerName = "CONTAINERNAME"; | |
var connectionString = "CONNECTIONSTRING"; | |
var sasBuilder = new ShareSasBuilder() | |
{ | |
ShareName = containerName, | |
Resource = "s", //Value b is for generating token for a Blob and c is for container | |
StartsOn = DateTime.UtcNow.AddMinutes(-2), | |
ExpiresOn = DateTime.UtcNow.AddMinutes(10), | |
}; | |
sasBuilder.SetPermissions(ShareSasPermissions.Read | ShareSasPermissions.Write| ShareSasPermissions.List); //multiple permissions can be added by using | symbol | |
var sasToken = sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(GetKeyValueFromConnectionString("AccountName"), GetKeyValueFromConnectionString("AccountKey"))); | |
//Console.WriteLine($"{new BlobContainerClient(connectionString, containerName).Uri}?{sasToken}"); | |
Console.WriteLine($"{new ShareClient(connectionString, containerName).Uri}?{sasToken}&restype=directory&comp=list"); | |
/* Note : If you want to list the items inside container and view those details in a browser based on the generated SAS token | |
* then two additional query parameters has to be appended to the token | |
* the Query parameters are "restype=container&comp=list" | |
*/ | |
} | |
} | |
class Run | |
{ | |
static void Main(string[] args) | |
{ | |
var class1 = new Program(); | |
class1.GetAdHocContainerSasToken(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment