private static async Task TestContainerSASAsync(string sasUri, string blobName, string blobContent)
{
// Try performing container operations with the SAS provided.
// Note that the storage account credentials are not required here; the SAS provides the necessary
// authentication information on the URI.
// Return a reference to the container using the SAS URI.
CloudBlobContainer container = new CloudBlobContainer(new Uri(sasUri));
// Return a reference to a blob to be created in the container.
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
// Write operation: Upload a new blob to the container.
try
{
MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
msWrite.Position = 0;
using (msWrite)
{
await blob.UploadFromStreamAsync(msWrite);
}
Console.WriteLine("Write operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Write operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// List operation: List the blobs in the container.
try
{
foreach (ICloudBlob blobItem in container.ListBlobs(
prefix: null,
useFlatBlobListing: true,
blobListingDetails: BlobListingDetails.None,
options: null,
operationContext: null))
{
Console.WriteLine(blobItem.Uri);
}
Console.WriteLine("List operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("List operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// Read operation: Read the contents of the blob we created above.
try
{
MemoryStream msRead = new MemoryStream();
msRead.Position = 0;
using (msRead)
{
await blob.DownloadToStreamAsync(msRead);
Console.WriteLine(msRead.Length);
}
Console.WriteLine();
Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Read operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
Console.WriteLine();
// Delete operation: Delete the blob we created above.
try
{
await blob.DeleteAsync();
Console.WriteLine("Delete operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Delete operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
Console.WriteLine();
}