-
Notifications
You must be signed in to change notification settings - Fork 1
/
AzureBlobSAS.cs
82 lines (61 loc) · 3.32 KB
/
AzureBlobSAS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Globalization;
using System.IO;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace AzureBlob
{
class AzureBlobSAS
{
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();
string containerName = "containername";
//Get a reference to a container to use for the sample code, and create it if it does not exist.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
//container.CreateIfNotExists();
//Insert calls to the methods created below here...
Console.WriteLine("Blob Container: " + containerName + "retrieved successfully. Press any key to continue... ");
Console.ReadLine();
#region Get SAS for Blob URL
string blobname = "BlobFileName.extension";
string accesskey = GetBlobSasUri(blobname, container);
#endregion
#region Create and set the Values in Text document
Console.WriteLine(FormatTheSASInfo(accesskey, blobname));
Console.WriteLine("Press Any Key to Exit...");
#endregion
//Require user input before closing the console window.
Console.ReadLine();
}
private static string FormatTheSASInfo(string accesskey, string blobname)
{
// Compose a string that consists of three lines.
string dtformat = "dd MMM yyyy hh:mm tt";
string lineExc = "DateTime:" + DateTime.Now.ToString(dtformat) +
Environment.NewLine + "Name: " + blobname +
Environment.NewLine + "Access Key: " + accesskey;
return lineExc;
}
static string GetBlobSasUri(string blobName, CloudBlobContainer container)
{
//Get a reference to a blob within the container.
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
//Set the expiry time and permissions for the blob.
//In this case, the start time is specified as a few minutes in the past, to mitigate clock skew.
//The shared access signature will be valid immediately.
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddYears(1);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List;
//Generate the shared access signature on the blob, setting the constraints directly on the signature.
string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);
//Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;
}
}
}