-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (100 loc) · 5.24 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (100 loc) · 5.24 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using System;
class Program
{
static void Main(string[] args)
{
string accountName = "<your-storage-account-name>";
string accountKey = "<your-storage-account-key>";
string containerName = "<your-container-name>"; // Specify the container name
string blobName = "<your-blob-name>"; // Specify the blob name to try deleting
// Create a StorageSharedKeyCredential using your account name and key
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
// Define the SAS token parameters with Read permission (no delete)
AccountSasPermissions permissions = AccountSasPermissions.Read | AccountSasPermissions.List; // Only Read permission
//DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(1);//1 hour 60 mins
//DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(.5);//half hour = 30 mins
//DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(.25);//15 mins
//DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(.123);//approx 7.5 mins
DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(.0625);//approx 3 mins
// Create the SAS token builder
AccountSasBuilder sasBuilder = new AccountSasBuilder()
{
Services = AccountSasServices.Blobs, // Allow SAS for Blob services
ResourceTypes = AccountSasResourceTypes.Service | AccountSasResourceTypes.Container | AccountSasResourceTypes.Object, // Allow for container and blob-level access
ExpiresOn = expiryTime, // Expiry of the SAS token
};
// Set the permissions on the SAS token
sasBuilder.SetPermissions(permissions);
// Generate the SAS token
string sasToken = sasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString();
// Construct the full URI with the SAS token
Uri CompletesasUri = new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{blobName}?{sasToken}");
Console.WriteLine("Complete SAS URI: " + CompletesasUri.ToString());
string blobServiceURI = $"https://{accountName}.blob.core.windows.net";
// Create a BlobServiceClient using the SAS URI
BlobServiceClient blobServiceClient = new BlobServiceClient(new Uri($"{blobServiceURI}?{sasToken}"));
Console.WriteLine("*******************");
// Call the method to list blobs in the container
ListBlobsInContainer(blobServiceClient, containerName);
Console.WriteLine("*******************");
// Check if the SAS token is valid and print the time left
CheckSasTokenValidity(sasBuilder);
Console.WriteLine("*******************");
// Try to delete a blob (should fail due to read-only permission)
TryDeleteBlob(blobServiceClient, containerName, blobName);
}
// Method to list blobs in a container
static void ListBlobsInContainer(BlobServiceClient blobServiceClient, string containerName)
{
try
{
// Get a reference to the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Console.WriteLine($"Listing blobs in container: {containerName}");
// List all blobs in the container
foreach (var blobItem in containerClient.GetBlobs())
{
Console.WriteLine($"Blob name: {blobItem.Name}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error listing blobs: {ex.Message}");
}
}
// Method to check the SAS token validity
static void CheckSasTokenValidity(AccountSasBuilder sasBuilder)
{
// Calculate the time left until the SAS token expires
TimeSpan timeLeft = sasBuilder.ExpiresOn - DateTimeOffset.UtcNow;
if (timeLeft.TotalSeconds > 0)
{
Console.WriteLine($"SAS Token is valid. Time left: {timeLeft.Hours} hours {timeLeft.Minutes} minutes.");
}
else
{
Console.WriteLine("SAS Token has expired.");
}
}
// Method to attempt to delete a blob (should fail due to read-only permission)
static void TryDeleteBlob(BlobServiceClient blobServiceClient, string containerName, string blobName)
{
try
{
// Get a reference to the container and the blob
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Try to delete the blob (this will fail due to read-only permission)
Console.WriteLine($"Attempting to delete blob: {blobName}");
blobClient.DeleteIfExists(); // This should fail because of the 'Read' permission
}
catch (Exception ex)
{
// This should fail with a permission error due to read-only SAS token
Console.WriteLine($"Error attempting to delete blob"); //{ex.Message}
}
}
}