88using Azure ;
99using Azure . Storage . Blobs ;
1010using Azure . Storage . Blobs . Specialized ;
11+ using Microsoft . Extensions . Logging ;
1112
1213namespace NuGet . Services . Storage
1314{
@@ -20,11 +21,11 @@ public class BlobLeaseService : IBlobLeaseService
2021 {
2122 private static readonly TimeSpan MinLeaseTime = TimeSpan . FromSeconds ( 15 ) ;
2223 private static readonly TimeSpan MaxLeaseTime = TimeSpan . FromSeconds ( 60 ) ;
23-
2424 private readonly BlobContainerClient _containerClient ;
2525 private readonly string _basePath ;
26+ private readonly bool _createBlobWhenMissing ;
2627
27- public BlobLeaseService ( BlobServiceClient blobServiceClient , string containerName , string basePath )
28+ public BlobLeaseService ( BlobServiceClient blobServiceClient , string containerName , string basePath , Boolean createBlobsWhenMissing = true )
2829 {
2930 if ( blobServiceClient == null )
3031 {
@@ -34,13 +35,13 @@ public BlobLeaseService(BlobServiceClient blobServiceClient, string containerNam
3435 {
3536 throw new ArgumentException ( "The container name must be provided." , nameof ( containerName ) ) ;
3637 }
37- if ( string . IsNullOrEmpty ( basePath ) )
38+ if ( basePath == null )
3839 {
3940 throw new ArgumentException ( "The base path must be provided." , nameof ( basePath ) ) ;
4041 }
41-
4242 _containerClient = blobServiceClient . GetBlobContainerClient ( containerName ) ;
4343 _basePath = string . IsNullOrEmpty ( basePath ) ? string . Empty : basePath . TrimEnd ( '/' ) + '/' ;
44+ _createBlobWhenMissing = createBlobsWhenMissing ;
4445 }
4546
4647 public async Task < BlobLeaseResult > TryAcquireAsync ( string resourceName , TimeSpan leaseTime , CancellationToken cancellationToken )
@@ -54,7 +55,11 @@ public async Task<BlobLeaseResult> TryAcquireAsync(string resourceName, TimeSpan
5455 catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotFound )
5556 {
5657 // The lease file does not exist. Try to create it and lease it.
57- return await TryCreateAndAcquireAsync ( blob , leaseTime , cancellationToken ) ;
58+ if ( _createBlobWhenMissing )
59+ {
60+ return await TryCreateAndAcquireAsync ( blob , leaseTime , cancellationToken ) ;
61+ }
62+ return BlobLeaseResult . Failure ( ) ;
5863 }
5964 }
6065
@@ -65,7 +70,6 @@ public async Task<bool> ReleaseAsync(string resourceName, string leaseId, Cancel
6570 var blob = GetBlob ( resourceName ) ;
6671 var leaseClient = blob . GetBlobLeaseClient ( leaseId ) ;
6772 await leaseClient . ReleaseAsync ( conditions : null , cancellationToken : cancellationToken ) ;
68-
6973 return true ;
7074 }
7175 catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . Conflict )
@@ -94,6 +98,7 @@ public async Task<BlobLeaseResult> RenewAsync(string resourceName, string leaseI
9498 try
9599 {
96100 var lease = await leaseClient . RenewAsync ( conditions : null , cancellationToken : cancellationToken ) ;
101+
97102 return BlobLeaseResult . Success ( lease ) ;
98103 }
99104 catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . Conflict || ex . Status == ( int ) HttpStatusCode . PreconditionFailed )
@@ -135,6 +140,7 @@ private async Task<BlobLeaseResult> TryAcquireAsync(BlobClient blob, TimeSpan le
135140 {
136141 var leaseClient = blob . GetBlobLeaseClient ( ) ;
137142 var blobLease = await leaseClient . AcquireAsync ( leaseTime , conditions : null , cancellationToken : cancellationToken ) ;
143+
138144 return BlobLeaseResult . Success ( blobLease ) ;
139145 }
140146 catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . Conflict )
0 commit comments