public async Task <IFileInformation> CopyFileToSameServiceType(string identifier, IStorageService destinationService) { var destination = (AzureStorageService)destinationService; var uniqueName = Guid.NewGuid().ToString(); CloudBlockBlob sourceBlob = null; try { sourceBlob = BlobContainer.GetBlockBlobReference(identifier); await sourceBlob.AcquireLeaseAsync(null); var destinationBlob = destination.BlobContainer.GetBlockBlobReference(uniqueName); await destinationBlob.StartCopyAsync(sourceBlob); } finally { // Break the lease on the source blob. if (sourceBlob != null) { await sourceBlob.FetchAttributesAsync(); if (sourceBlob.Properties.LeaseState != LeaseState.Available) { await sourceBlob.BreakLeaseAsync(new TimeSpan(0)); } } } var info = new AzureFileInformation { StorageIdentifier = uniqueName }; return(info); }
/// <summary> /// Gets a reference to a blob created previously, and copies it to a new blob in the same container. /// </summary> /// <param name="container">A CloudBlobContainer object.</param> /// <param name="sourceBlobName"></param> /// <param name="destinationBlobName"></param> /// <returns>A Task object.</returns> private static async Task CopyBlockBlobAsync(CloudBlobContainer container, string sourceBlobName, string destinationBlobName) { CloudBlockBlob sourceBlob = null; CloudBlockBlob destBlob = null; string leaseId = null; try { // Get a block blob from the container to use as the source. sourceBlob = container.GetBlockBlobReference(sourceBlobName); // Lease the source blob for the copy operation to prevent another client from modifying it. // Specifying null for the lease interval creates an infinite lease. leaseId = await sourceBlob.AcquireLeaseAsync(null); // Get a reference to a destination blob (in this case, a new blob). destBlob = container.GetBlockBlobReference(destinationBlobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Get the ID of the copy operation. string copyId = await destBlob.StartCopyAsync(sourceBlob); // Fetch the destination blob's properties before checking the copy state. await destBlob.FetchAttributesAsync(); Console.WriteLine("Status of copy operation: {0}", destBlob.CopyState.Status); Console.WriteLine("Completion time: {0}", destBlob.CopyState.CompletionTime); Console.WriteLine("Bytes copied: {0}", destBlob.CopyState.BytesCopied.ToString()); Console.WriteLine("Total bytes: {0}", destBlob.CopyState.TotalBytes.ToString()); Console.WriteLine(); } } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } finally { // Break the lease on the source blob. if (sourceBlob != null) { await sourceBlob.FetchAttributesAsync(); if (sourceBlob.Properties.LeaseState != LeaseState.Available) { await sourceBlob.BreakLeaseAsync(new TimeSpan(0)); } } } }
private static async Task releaseLease(CloudBlockBlob sourceBlob) { // Break the lease on the source blob. if (sourceBlob != null) { if (await sourceBlob.ExistsAsync()) { await sourceBlob.FetchAttributesAsync(); if (sourceBlob.Properties.LeaseState != LeaseState.Available) { await sourceBlob.BreakLeaseAsync(new TimeSpan(0)); } } } }
public Task BreakLeaseAsync(TimeSpan?breakPeriod) { return(m_blob.BreakLeaseAsync(breakPeriod)); }