-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAzureBlobManager.cs
More file actions
78 lines (70 loc) · 2.61 KB
/
Copy pathAzureBlobManager.cs
File metadata and controls
78 lines (70 loc) · 2.61 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
using Azure.Storage.Blobs;
namespace MigrateABStoSPE
{
public class AzureBlobManager
{
string _containerLevelSASUrl;
BlobContainerClient _containerClient;
public AzureBlobManager(string containerLevelSASUrl)
{
_containerLevelSASUrl = containerLevelSASUrl;
_containerClient = new BlobContainerClient(new Uri(_containerLevelSASUrl));
}
/// <summary>
/// Gets the name of the Azure Blob Storage container.
/// </summary>
/// <returns>The name of the container.</returns>
public string GetContainerName()
{
return _containerClient.Name;
}
/// <summary>
/// Lists all the blobs in the Azure Blob Storage container asynchronously.
/// </summary>
/// <returns>An enumerable collection of blob names or null.</returns>
public async Task<IEnumerable<string>?> ListBlobsAsync()
{
try
{
var blobs = new List<string>();
await foreach (var blobItem in _containerClient.GetBlobsAsync())
{
blobs.Add(blobItem.Name);
}
return blobs;
}
catch (Exception ex)
{
Utility.ConsoleWriteWithColor($"An error occurred while listing blobs: {ex.Message}", ConsoleColor.Red);
return null;
}
}
/// <summary>
/// Downloads the stream of a blob from Azure Blob Storage.
/// </summary>
/// <param name="blobName">The name of the blob to download.</param>
/// <returns>The stream containing the downloaded blob data or null.</returns>
public async Task<Stream> DownloadBlobStreamAsync(string blobName)
{
try
{
BlobClient blobClient = _containerClient.GetBlobClient(blobName);
MemoryStream memoryStream = new MemoryStream();
await blobClient.DownloadToAsync(memoryStream);
memoryStream.Position = 0; // Reset the stream position to the beginning
return memoryStream;
}
catch (Exception ex)
{
string message = ex.Message;
int index = message.IndexOf("RequestId");
if (index != -1)
{
message = message.Substring(0, index);
}
Utility.ConsoleWriteWithColor($"An error occurred while downloading blob: {message}", ConsoleColor.Red);
throw ex;
}
}
}
}