BlobStorage.Advanced.CopyLargeBlockBlobAsync C# (CSharp) Method

CopyLargeBlockBlobAsync() private static method

Creates a large block blob, and copies it to a new blob in the same container. If the copy operation does not complete within the specified interval, abort the copy operation.
private static CopyLargeBlockBlobAsync ( Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container, int sizeInMb ) : System.Threading.Tasks.Task
container Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer A CloudBlobContainer object.
sizeInMb int The size of block blob to create, in MB.
return System.Threading.Tasks.Task
        private static async Task CopyLargeBlockBlobAsync(CloudBlobContainer container, int sizeInMb)
        {
            // Create an array of random bytes, of the specified size.
            byte[] bytes = new byte[sizeInMb * 1024 * 1024];
            Random rng = new Random();
            rng.NextBytes(bytes);

            // Get a reference to a new block blob.
            CloudBlockBlob sourceBlob = container.GetBlockBlobReference("LargeSourceBlob");

            // Get a reference to the destination blob (in this case, a new blob).
            CloudBlockBlob destBlob = container.GetBlockBlobReference("copy of " + sourceBlob.Name);

            MemoryStream msWrite = null;
            string copyId = null;
            string leaseId = null;

            try
            {
                // Create a new block blob comprised of random bytes to use as the source of the copy operation.
                msWrite = new MemoryStream(bytes);
                msWrite.Position = 0;
                using (msWrite)
                {
                    await sourceBlob.UploadFromStreamAsync(msWrite);
                }

                // 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 the ID of the copy operation.
                copyId = await destBlob.StartCopyAsync(sourceBlob);

                // Fetch the destination blob's properties before checking the copy state.
                await destBlob.FetchAttributesAsync();

                // Sleep for 1 second. In a real-world application, this would most likely be a longer interval.
                System.Threading.Thread.Sleep(1000);

                // Check the copy status. If it is still pending, abort the copy operation.
                if (destBlob.CopyState.Status == CopyStatus.Pending)
                {
                    await destBlob.AbortCopyAsync(copyId);
                    Console.WriteLine("Copy operation {0} has been aborted.", copyId);
                }

                Console.WriteLine();
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
            finally
            {
                // Close the stream.
                if (msWrite != null)
                {
                    msWrite.Close();
                }

                // 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));
                    }
                }

                // Delete the source blob.
                if (sourceBlob != null)
                {
                    await sourceBlob.DeleteIfExistsAsync();
                }

                // Delete the destination blob.
                if (destBlob != null)
                {
                    await destBlob.DeleteIfExistsAsync();
                }
            }
        }