Skip to content

Commit 4e56f6b

Browse files
authored
Merge pull request #4 from SamhammerAG/SSP-4411-fix-prefix-deletion
Add slash to foldername to not delete folders which names starts with…
2 parents ec407c4 + 9d9a179 commit 4e56f6b

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/Samhammer.AzureBlobStorage.Test/IntegrationTest.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public IntegrationTest()
3838
clientFactory.GetDefaultContainerName().Returns(DefaultContainerName);
3939
clientFactory.GetClient().Returns(new BlobServiceClient(ConnectionString));
4040
var options = Substitute.For<IOptions<AzureBlobStorageOptions>>();
41-
var streamManagerService = Substitute.For<IStreamManagerService>();
41+
42+
var streamManagerOptions = new StreamManagerOptions();
43+
var iOptionsStreamManager = Microsoft.Extensions.Options.Options.Create(streamManagerOptions);
44+
var streamManagerService = new StreamManagerService(iOptionsStreamManager);
4245

4346
_service = new AzureBlobStorageService<IAzureBlobStorageClientFactory>(clientFactory, streamManagerService, options);
4447

@@ -129,6 +132,54 @@ public async Task TestEntireProcess(string containerName)
129132

130133
filesAfterDeletion.Count.Should().Be(0);
131134

135+
// Delete folder and verify only folder with prefix is gone, others kept
136+
testFileReadStream.Seek(0, SeekOrigin.Begin);
137+
await _service.UploadBlobAsync(testFileName, testFileContentType, testFileReadStream, containerName, "1");
138+
testFileReadStream.Seek(0, SeekOrigin.Begin);
139+
await _service.UploadBlobAsync(testFileName, testFileContentType, testFileReadStream, containerName, "10");
140+
testFileReadStream.Seek(0, SeekOrigin.Begin);
141+
await _service.UploadBlobAsync(testFileName, testFileContentType, testFileReadStream, containerName, "11");
142+
await _service.DeleteFolderAsync("1", containerName);
143+
144+
var filesAfterFolderDeletion = await _service.ListBlobsInContainerAsync(containerName).ToListAsync();
145+
146+
filesAfterFolderDeletion.Should().ContainEquivalentOf(
147+
new BlobInfoContract
148+
{
149+
Name = "10/" + testFileName,
150+
AccessTier = "Hot",
151+
BlobType = "Block",
152+
ContentEncoding = string.Empty,
153+
ContentType = testFileContentType,
154+
DateCreated = DateTimeOffset.UtcNow,
155+
Size = 1021702,
156+
},
157+
_comparisonOptions)
158+
.And.ContainEquivalentOf(
159+
new BlobInfoContract
160+
{
161+
Name = "11/" + testFileName,
162+
AccessTier = "Hot",
163+
BlobType = "Block",
164+
ContentEncoding = string.Empty,
165+
ContentType = testFileContentType,
166+
DateCreated = DateTimeOffset.UtcNow,
167+
Size = 1021702,
168+
},
169+
_comparisonOptions)
170+
.And.NotContainEquivalentOf(
171+
new BlobInfoContract
172+
{
173+
Name = "1/" + testFileName,
174+
AccessTier = "Hot",
175+
BlobType = "Block",
176+
ContentEncoding = string.Empty,
177+
ContentType = testFileContentType,
178+
DateCreated = DateTimeOffset.UtcNow,
179+
Size = 1021702,
180+
},
181+
_comparisonOptions);
182+
132183
// Delete container and verify it's gone
133184
await _service.DeleteContainerAsync(containerName);
134185
var containersAfterDeletion = await _service.GetContainersAsync().ToListAsync();

src/Samhammer.AzureBlobStorage/Services/AzureBlobStorageService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private Uri CreateServiceSASBlob(BlobClient blobClient)
119119
Resource = "b",
120120
};
121121

122-
sasBuilder.ExpiresOn = _blobStorageOptions.Value.FileUrlExpires.HasValue
122+
sasBuilder.ExpiresOn = _blobStorageOptions.Value.FileUrlExpires.HasValue
123123
? DateTimeOffset.UtcNow.Add(_blobStorageOptions.Value.FileUrlExpires.Value)
124124
: DateTimeOffset.UtcNow.AddDays(1);
125125

@@ -147,7 +147,8 @@ public async Task DeleteBlobAsync(string blobName, string containerName = null)
147147
public async Task DeleteFolderAsync(string folderName, string containerName = null)
148148
{
149149
var containerClient = await GetContainerClient(containerName);
150-
var blobs = containerClient.GetBlobsAsync(prefix: folderName);
150+
folderName = folderName.TrimEnd('/');
151+
var blobs = containerClient.GetBlobsAsync(prefix: $"{folderName}/");
151152

152153
await foreach (var blob in blobs)
153154
{

0 commit comments

Comments
 (0)