-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
95 lines (86 loc) · 4.15 KB
/
Program.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
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace blob_app
{
class Program
{
static void Main(string[] args)
{
BlobOperations().GetAwaiter().GetResult();
}
public static async Task BlobOperations()
{
string connectionString = Environment.GetEnvironmentVariable("storageconnectionstring");
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(connectionString, out storageAccount))
{
try
{
// Create blob container if not already created
Console.WriteLine("Creating blob container");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("my-container");
await blobContainer.CreateIfNotExistsAsync(); //To-do handle exception Microsoft.WindowsAzure.Storage.StorageException
// Set access on the blob container
BlobContainerPermissions blobPermissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
await blobContainer.SetPermissionsAsync(blobPermissions);
// Create a file for upload
Console.WriteLine("Creating temp file");
string currentDir = Directory.GetCurrentDirectory();
string fileName = "hello.txt";
string file = Path.Combine(currentDir, fileName);
await File.WriteAllTextAsync(file, "Hello, World!");
// Upload the file to the blob container
Console.WriteLine("Uploading file");
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
await blockBlob.UploadFromFileAsync(file);
// List blobs in the container
Console.WriteLine("Listing blobs in container:");
BlobContinuationToken token = null;
do
{
var results = await blobContainer.ListBlobsSegmentedAsync(null, token);
token = results.ContinuationToken;
foreach (IListBlobItem item in results.Results)
{
Console.WriteLine("- " + item.Uri);
}
} while (token != null);
// Download the file from blob container
Console.WriteLine("Downloading file");
string downloadFile = file.Replace(".txt", "_downloaded.txt");
await blockBlob.DownloadToFileAsync(downloadFile, FileMode.Create);
// Delete blob container and files
Console.WriteLine("Blob container created, file uploaded, and copy downloaded.\n" +
"Hit any key to delete resources.");
Console.ReadLine();
if (blobContainer != null)
{
await blobContainer.DeleteIfExistsAsync();
}
File.Delete(file);
File.Delete(downloadFile);
Console.WriteLine("Blob container and files have been deleted.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
else
{
Console.WriteLine("The environment variable storageconnectionstring is not set."
+ "\nPlease set it and rerun this program.");
Console.WriteLine("Hit any key to exit this program");
Console.ReadLine();
}
}
}
}