EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

CloudBlockBlob.AbortCopyAsync Method

 

Updated: December 20, 2016

Initiates an asynchronous operation to abort an ongoing blob copy operation.

Namespace:   Microsoft.WindowsAzure.Storage.Blob
Assembly:  Microsoft.WindowsAzure.Storage (in Microsoft.WindowsAzure.Storage.dll)

NameDescription
System_CAPS_pubmethodAbortCopyAsync(String)

Initiates an asynchronous operation to abort an ongoing blob copy operation. (Inherited from CloudBlob.)

System_CAPS_pubmethodAbortCopyAsync(String, AccessCondition, BlobRequestOptions, OperationContext)

Initiates an asynchronous operation to abort an ongoing blob copy operation. (Inherited from CloudBlob.)

System_CAPS_pubmethodAbortCopyAsync(String, AccessCondition, BlobRequestOptions, OperationContext, CancellationToken)

Initiates an asynchronous operation to abort an ongoing blob copy operation. (Inherited from CloudBlob.)

System_CAPS_pubmethodAbortCopyAsync(String, CancellationToken)

Initiates an asynchronous operation to abort an ongoing blob copy operation. (Inherited from CloudBlob.)

The AbortCopyAsync method aborts a copy operation, leaving a destination blob with zero length and complete metadata. Use the copy ID returned by the StartCopyAsync method to abort the copy operation.

For more information about aborting a copy operation, see Abort Copy Blob.

This code example creates a new block blob, then copies it to a another blob. If the copy operation is still pending after one second, the copy operation is aborted.

private static async Task CopyBlockBlobAsync(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();
        }
    }
}

Return to top
Weergeven: