/// <summary> /// Abort copy operation on specified blob /// </summary> /// <param name="blob">CloudBlob object</param> /// <param name="copyId">Copy id</param> /// <param name="accessCondition">Access condition</param> /// <param name="options">Blob request options</param> /// <param name="operationContext">Operation context</param> public void AbortCopy(CloudBlob blob, string copyId, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) { try { blob.AbortCopy(copyId, accessCondition, options, operationContext); } catch (StorageException e) { if (e.IsSuccessfulResponse()) { //The abort operation is successful, although get an exception return; } else { throw; } } }
public void GetCopyStateWhenCopyingTest() { CloudBlobContainer Container = blobUtil.CreateContainer(); string ContainerName = Container.Name; string BlobName = Utility.GenNameString("blob"); CloudBlob Blob = blobUtil.CreateBlockBlob(Container, BlobName); string uri = Test.Data.Get("BigBlobUri"); Test.Assert(!String.IsNullOrEmpty(uri), string.Format("Big file uri should be not empty, actually it's {0}", uri)); if (String.IsNullOrEmpty(uri)) { return; } Blob.StartCopy(new Uri(uri)); int maxMonitorCount = 3; int checkCount = 0; int sleepInterval = 1000; //ms CopyStatus status = CopyStatus.Pending; try { int expectedCopyStateCount = 1; do { Test.Info(String.Format("{0}th check current copy state", checkCount)); Test.Assert(CommandAgent.GetAzureStorageBlobCopyState(ContainerName, BlobName, false), "Get copy state in dest container should succeed."); Test.Assert(CommandAgent.Output.Count == expectedCopyStateCount, String.Format("Should contain {0} copy state, and actually it's {1}", expectedCopyStateCount, CommandAgent.Output.Count)); if (lang == Language.PowerShell) { status = (CopyStatus)CommandAgent.Output[0]["Status"]; Test.Assert(status == CopyStatus.Pending, String.Format("Copy status should be Pending, actually it's {0}", status)); } else { if (((JObject)CommandAgent.Output[0]["copy"])["status"].ToString().Equals("pending")) { status = CopyStatus.Pending; } else { status = CopyStatus.Invalid; } } checkCount++; Thread.Sleep(sleepInterval); }while (status == CopyStatus.Pending && checkCount < maxMonitorCount); Test.Info("Finish the monitor loop and try to abort copy"); try { Blob.AbortCopy(Blob.CopyState.CopyId); } catch (StorageException e) { //TODO use extension method if (e.RequestInformation != null && e.RequestInformation.HttpStatusCode == 409) { Test.Info("Skip 409 abort conflict exception. Error:{0}", e.Message); Test.Info("Detail Error Message: {0}", e.RequestInformation.HttpStatusMessage); } else { Test.AssertFail(String.Format("Can't abort copy. Error: {0}", e.Message)); } } Test.Assert(CommandAgent.GetAzureStorageBlobCopyState(ContainerName, BlobName, false), "Get copy state in dest container should succeed."); Test.Assert(CommandAgent.Output.Count == expectedCopyStateCount, String.Format("Should contain {0} copy state, and actually it's {1}", expectedCopyStateCount, CommandAgent.Output.Count)); if (lang == Language.PowerShell) { status = (CopyStatus)CommandAgent.Output[0]["Status"]; } else { if (((JObject)CommandAgent.Output[0]["copy"])["status"].ToString().Equals("aborted")) { status = CopyStatus.Aborted; } else { status = CopyStatus.Pending; } } Test.Assert(status == CopyStatus.Aborted, String.Format("Copy status should be Aborted, actually it's {0}", status)); } finally { blobUtil.RemoveContainer(Container.Name); } }