-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlobstorageHelper.cs
More file actions
131 lines (107 loc) · 5.39 KB
/
BlobstorageHelper.cs
File metadata and controls
131 lines (107 loc) · 5.39 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
119
120
121
122
123
124
125
126
127
128
129
130
131
namespace Common;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Microsoft.Extensions.Logging;
using Model;
public class BlobStorageHelper : IBlobStorageHelper
{
private readonly ILogger<BlobStorageHelper> _logger;
public BlobStorageHelper(ILogger<BlobStorageHelper> logger)
{
_logger = logger;
}
public async Task CopyFileToPoisonAsync(string connectionString, string fileName, string containerName)
{
var sourceBlobServiceClient = new BlobServiceClient(connectionString);
var sourceContainerClient = sourceBlobServiceClient.GetBlobContainerClient(containerName);
var sourceBlobClient = sourceContainerClient.GetBlobClient(fileName);
BlobLeaseClient sourceBlobLease = new(sourceBlobClient);
var destinationBlobServiceClient = new BlobServiceClient(connectionString);
var destinationContainerClient = destinationBlobServiceClient.GetBlobContainerClient(Environment.GetEnvironmentVariable("fileExceptions"));
var destinationBlobClient = destinationContainerClient.GetBlobClient(fileName);
await destinationContainerClient.CreateIfNotExistsAsync(PublicAccessType.None);
try
{
await sourceBlobLease.AcquireAsync(BlobLeaseClient.InfiniteLeaseDuration);
await destinationBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
}
catch (RequestFailedException ex)
{
_logger.LogError(ex, "There has been a problem while copying the file: {Message}", ex.Message);
throw;
}
finally
{
await sourceBlobLease.ReleaseAsync();
}
}
public async Task CopyFileToPoisonAsync(string connectionString, string fileName, string containerName, string poisonContainerName, bool addTimestamp = false)
{
var sourceBlobServiceClient = new BlobServiceClient(connectionString);
var sourceContainerClient = sourceBlobServiceClient.GetBlobContainerClient(containerName);
var sourceBlobClient = sourceContainerClient.GetBlobClient(fileName);
BlobLeaseClient sourceBlobLease = new(sourceBlobClient);
var destinationBlobServiceClient = new BlobServiceClient(connectionString);
var destinationContainerClient = destinationBlobServiceClient.GetBlobContainerClient(poisonContainerName);
// Conditionally add timestamp to prevent collisions and maintain audit trail
var destinationFileName = fileName;
if (addTimestamp)
{
var timestamp = DateTime.UtcNow.ToString("yyyyMMdd_HHmmss");
var fileExtension = Path.GetExtension(fileName);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
destinationFileName = $"{fileNameWithoutExtension}_{timestamp}{fileExtension}";
}
var destinationBlobClient = destinationContainerClient.GetBlobClient(destinationFileName);
await destinationContainerClient.CreateIfNotExistsAsync(PublicAccessType.None);
try
{
await sourceBlobLease.AcquireAsync(BlobLeaseClient.InfiniteLeaseDuration);
await destinationBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
}
catch (RequestFailedException ex)
{
_logger.LogError(ex, "There has been a problem while copying the file: {Message}", ex.Message);
throw new InvalidOperationException($"Failed to copy file '{fileName}' from container '{containerName}' to poison container as '{destinationFileName}'.", ex);
}
finally
{
await sourceBlobLease.ReleaseAsync();
}
}
public async Task<bool> UploadFileToBlobStorage(string connectionString, string containerName, BlobFile blobFile, bool overwrite = false)
{
var sourceBlobServiceClient = new BlobServiceClient(connectionString);
var sourceContainerClient = sourceBlobServiceClient.GetBlobContainerClient(containerName);
var sourceBlobClient = sourceContainerClient.GetBlobClient(blobFile.FileName);
await sourceContainerClient.CreateIfNotExistsAsync(PublicAccessType.None);
try
{
await sourceBlobClient.UploadAsync(blobFile.Data, overwrite: overwrite);
}
catch (Exception ex)
{
_logger.LogError(ex, "There has been a problem while uploading the file: {Message}", ex.Message);
return false;
}
return true;
}
public async Task<BlobFile> GetFileFromBlobStorage(string connectionString, string containerName, string fileName)
{
_logger.LogInformation("Downloading File: {FileName} From blobStorage Container: {ContainerName}", fileName, containerName);
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(fileName);
await containerClient.CreateIfNotExistsAsync(PublicAccessType.None);
if (await blobClient.ExistsAsync())
{
var stream = new MemoryStream();
await blobClient.DownloadToAsync(stream);
return new BlobFile(stream, fileName);
}
_logger.LogWarning("File {FileName} does not exist in blobStorageContainer: {ContainerName}", fileName, containerName);
return null;
}
}