public async Task <BlobContainerInfo> CloneContainer(string sourceContainerName, string targetContainerName) { try { // create target container BlobContainerClient targetContainerClient = new BlobContainerClient(connectionString, targetContainerName); BlobContainerInfo targetContainer = await targetContainerClient.CreateAsync(); // get source container BlobContainerClient sourceContainerClient = blobServiceClient.GetBlobContainerClient(sourceContainerName); // blobs from source container await foreach (BlobItem blob in sourceContainerClient.GetBlobsAsync()) { // create a blob client for the source blob BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation // to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. await lease.AcquireAsync(TimeSpan.FromSeconds(60)); // Get a BlobClient representing the destination blob with a unique name. BlobClient destBlob = targetContainerClient.GetBlobClient(sourceBlob.Name); // Start the copy operation. await destBlob.StartCopyFromUriAsync(sourceBlob.Uri); // Update the source blob's properties. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); } } } return(targetContainer); } catch (RequestFailedException e) { Console.WriteLine("HTTP error code {0}: {1}", e.Status, e.ErrorCode); Console.WriteLine(e.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public BlobLeaseClient GetBlobLeaseClient(BlobClient blobClient, string leaseId = null) { if (blobClient == null) { throw new ArgumentNullException(nameof(blobClient)); } return(blobClient.GetBlobLeaseClient(leaseId)); }
private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo info, ILogger log) { try { // Get the name of the first blob in the container to use as the source. string blobName = info.fileName; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. //await lease.AcquireAsync(TimeSpan.FromSeconds(100)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); log.LoggerInfo($"Lease state: {sourceProperties.LeaseState}", info); Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null); // Get a BlobClient representing the destination blob BlobClient destBlob = destContainer.GetBlobClient(blobName);//destContainer.GetBlobClient(blob_sas_uri.ToString()); // Start the copy operation. await destBlob.StartCopyFromUriAsync(blob_sas_uri); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); } } } catch (RequestFailedException ex) { log.LoggerError($"RequestFailedException: {ex.Message}", ex?.StackTrace, info); Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }
public void TestAttemptToLeaseBlobIfDoesNotExist() { var blobClient = new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, Guid.NewGuid().ToString()); Assert.Throws <RequestFailedException>(() => blobClient.GetBlobLeaseClient().Acquire(TimeSpan.FromMinutes(1))) .ErrorCode.ShouldEqual(AzureErrors.BlobNotFound); blobClient = new BlobClient(AzureCredentials.ConnectionString, "dne-container", Guid.NewGuid().ToString()); Assert.Throws <RequestFailedException>(() => blobClient.GetBlobLeaseClient().Acquire(TimeSpan.FromMinutes(1))) .ErrorCode.ShouldEqual("ContainerNotFound"); }
public void TestThrowsIfLeaseAlreadyHeld() { var name = nameof(TestThrowsIfLeaseAlreadyHeld) + Guid.NewGuid(); var client1 = new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name); client1.Upload(Stream.Null); var client2 = new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name); Assert.DoesNotThrow(() => client1.GetBlobLeaseClient().Acquire(TimeSpan.FromSeconds(15))); Assert.Throws <RequestFailedException>(() => client2.GetBlobLeaseClient().Acquire(TimeSpan.FromSeconds(15))) .ErrorCode.ShouldEqual("LeaseAlreadyPresent"); }
static void AcquireLease() { //https://manojchoudhari.wordpress.com/2019/12/06/implementing-azure-blob-leasing-using-net-sdk/ BlobContainerClient containerClient = client.GetBlobContainerClient(containerName); BlobClient blob = containerClient.GetBlobClient(filename); BlobLeaseClient leaseClient = blob.GetBlobLeaseClient(); TimeSpan ts = new TimeSpan(0, 0, 0, 30); leaseClient.AcquireAsync(ts); Azure.Response <BlobLease> blobLeaseResponse = leaseClient.Acquire(ts); Console.WriteLine("Blob Lease Id:" + blobLeaseResponse.Value.LeaseId); Console.WriteLine("Remaining Lease Time: " + blobLeaseResponse.Value.LeaseTime); Console.WriteLine("Acquired lease, Press ENTER to continue"); }
private static async Task <BlobLease> TryAcquireLeaseAsync( BlobClient blob, TimeSpan leasePeriod, CancellationToken cancellationToken) { var leaseClient = blob.GetBlobLeaseClient(); try { // Optimistically try to acquire the lease. The blob may not yet // exist. If it doesn't we handle the 404, create it, and retry below return(await leaseClient.AcquireAsync(leasePeriod, cancellationToken : cancellationToken).ConfigureAwait(false)); } catch (RequestFailedException exception) { switch (exception.Status) { case 409: return(null); case 404: break; default: throw; } } //404 await TryCreateAsync(blob, cancellationToken).ConfigureAwait(false); try { return(await leaseClient.AcquireAsync(leasePeriod, cancellationToken : cancellationToken).ConfigureAwait(false)); } catch (RequestFailedException exception) { if (exception.Status == 409) { return(null); } throw; } }
private static async Task CopyBlob() { blobClient = containerClient.GetBlobClient(fileName); if (await blobClient.ExistsAsync()) { BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. await leaseClient.AcquireAsync(TimeSpan.FromSeconds(-1)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await blobClient.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); // Get a BlobClient representing the destination blob with a unique name. BlobClient destBlob = containerClient.GetBlobClient(Guid.NewGuid() + "-" + blobClient.Name); // Start the copy operation. await destBlob.StartCopyFromUriAsync(blobClient.Uri); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); Console.WriteLine($"Copy status: {destProperties.CopyStatus}"); Console.WriteLine($"Copy progress: {destProperties.CopyProgress}"); Console.WriteLine($"Completion time: {destProperties.CopyCompletedOn}"); Console.WriteLine($"Total bytes: {destProperties.ContentLength}"); // Update the source blob's properties. sourceProperties = await blobClient.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await leaseClient.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await blobClient.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); } } }
/// <inheritdoc/> public async Task <string> TryAcquireBlobLease(string filepath) { BlobClient blobClient = CreateBlobClient(filepath); BlobLeaseClient blobLeaseClient = blobClient.GetBlobLeaseClient(); try { BlobLease blobLease = await blobLeaseClient.AcquireAsync(TimeSpan.FromSeconds(_storageConfig.BlobLeaseTimeout)); return(blobLease.LeaseId); } catch (RequestFailedException ex) { _logger.LogError(ex, "Failed to acquire blob lease for policy file at {filepath}. RequestFailedException", filepath); } catch (Exception ex) { _logger.LogError(ex, "Failed to acquire blob lease for policy file at {filepath}. Unexpected error", filepath); } return(null); }
private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo fileInfo) { try { // Get the name of the first blob in the container to use as the source. string blobName = fileInfo.fileName; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. //await lease.AcquireAsync(TimeSpan.FromSeconds(100)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null); // Get a BlobClient representing the destination blob BlobClient destBlob = destContainer.GetBlobClient(fileInfo.fileName);//destContainer.GetBlobClient(blob_sas_uri.ToString()); var dict = new Dictionary <string, string>(); foreach (var(tag, index) in fileInfo.tags.Split(",").WithIndex()) { dict.Add($"tag{index}", tag.Trim()); } dict.Add("correlationID", fileInfo.correlationID); dict.Add("source", fileInfo.source); dict.Add("description", fileInfo.description); var options = new BlobCopyFromUriOptions { Metadata = dict }; // Start the copy operation. await destBlob.StartCopyFromUriAsync(blob_sas_uri, options); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); } } } catch (RequestFailedException ex) { Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }
/// <inheritdoc/> public async void ReleaseBlobLease(string filepath, string leaseId) { BlobClient blobClient = CreateBlobClient(filepath); BlobLeaseClient blobLeaseClient = blobClient.GetBlobLeaseClient(leaseId); await blobLeaseClient.ReleaseAsync(); }
// </Snippet_DemonstrateOptimisticConcurrencyBlob> #endregion #region DemonstratePessimisticConcurrencyBlob //------------------------------------------------- // Demonstrate pessimistic concurrency for write operations to a blob //------------------------------------------------- // <Snippet_DemonstratePessimisticConcurrencyBlob> public static async Task DemonstratePessimisticConcurrencyBlob(BlobClient blobClient) { Console.WriteLine("Demonstrate pessimistic concurrency"); BlobContainerClient containerClient = blobClient.GetParentBlobContainerClient(); BlobLeaseClient blobLeaseClient = blobClient.GetBlobLeaseClient(); try { // Create the container if it does not exist. await containerClient.CreateIfNotExistsAsync(); // Upload text to a blob. string blobContents1 = "First update. Overwrite blob if it exists."; byte[] byteArray = Encoding.ASCII.GetBytes(blobContents1); using (MemoryStream stream = new MemoryStream(byteArray)) { BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, overwrite : true); } // Acquire a lease on the blob. BlobLease blobLease = await blobLeaseClient.AcquireAsync(TimeSpan.FromSeconds(15)); Console.WriteLine("Blob lease acquired. LeaseId = {0}", blobLease.LeaseId); // Set the request condition to include the lease ID. BlobUploadOptions blobUploadOptions = new BlobUploadOptions() { Conditions = new BlobRequestConditions() { LeaseId = blobLease.LeaseId } }; // Write to the blob again, providing the lease ID on the request. // The lease ID was provided, so this call should succeed. string blobContents2 = "Second update. Lease ID provided on request."; byteArray = Encoding.ASCII.GetBytes(blobContents2); using (MemoryStream stream = new MemoryStream(byteArray)) { BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, blobUploadOptions); } // This code simulates an update by another client. // The lease ID is not provided, so this call fails. string blobContents3 = "Third update. No lease ID provided."; byteArray = Encoding.ASCII.GetBytes(blobContents3); using (MemoryStream stream = new MemoryStream(byteArray)) { // This call should fail with error code 412 (Precondition Failed). BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream); } } catch (RequestFailedException e) { if (e.Status == (int)HttpStatusCode.PreconditionFailed) { Console.WriteLine( @"Precondition failure as expected. The lease ID was not provided."); } else { Console.WriteLine(e.Message); throw; } } finally { await blobLeaseClient.ReleaseAsync(); } }
//------------------------------------------------- // Copy a blob //------------------------------------------------- // <Snippet_CopyBlob> private static async Task CopyBlobAsync(BlobContainerClient container) { try { // Get the name of the first blob in the container to use as the source. string blobName = container.GetBlobs().FirstOrDefault().Name; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation // to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. await lease.AcquireAsync(TimeSpan.FromSeconds(-1)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); // Get a BlobClient representing the destination blob with a unique name. BlobClient destBlob = container.GetBlobClient(Guid.NewGuid() + "-" + sourceBlob.Name); // Start the copy operation. await destBlob.StartCopyFromUriAsync(sourceBlob.Uri); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); Console.WriteLine($"Copy status: {destProperties.CopyStatus}"); Console.WriteLine($"Copy progress: {destProperties.CopyProgress}"); Console.WriteLine($"Completion time: {destProperties.CopyCompletedOn}"); Console.WriteLine($"Total bytes: {destProperties.ContentLength}"); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); } } } catch (RequestFailedException ex) { Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }