private async Task StartCopyFromUri(long taskId, IStorageBlobManagement destChannel, Uri srcUri, BlobBaseClient destBlob) { bool destExist = true; Track2Models.BlobType? destBlobType = Util.GetBlobType(destBlob); Track2Models.BlobProperties properties = null; try { properties = (await destBlob.GetPropertiesAsync(this.BlobRequestConditions, cancellationToken: this.CmdletCancellationToken).ConfigureAwait(false)).Value; destBlobType = properties.BlobType; } catch (global::Azure.RequestFailedException e) when(e.Status == 404) { destExist = false; } if (destBlobType != null) { ValidateBlobTier(Util.convertBlobType_Track2ToTrack1(destBlobType), pageBlobTier, standardBlobTier, rehydratePriority); } if (!destExist || this.ConfirmOverwrite(srcUri.AbsoluteUri.ToString(), destBlob.Uri.ToString())) { Track2Models.BlobCopyFromUriOptions options = new global::Azure.Storage.Blobs.Models.BlobCopyFromUriOptions(); // The Blob Type and Blob Tier must match, since already checked they are match at the begin of ExecuteCmdlet(). if (pageBlobTier != null) { options.AccessTier = Util.ConvertAccessTier_Track1ToTrack2(pageBlobTier); } else if (standardBlobTier != null || rehydratePriority != null) { options.AccessTier = Util.ConvertAccessTier_Track1ToTrack2(standardBlobTier); options.RehydratePriority = Util.ConvertRehydratePriority_Track1ToTrack2(rehydratePriority); } if (this.BlobTag != null) { options.Tags = this.BlobTag.Cast <DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value); } options.SourceConditions = this.BlobRequestConditions; if (this.DestTagCondition != null) { options.DestinationConditions = new Track2Models.BlobRequestConditions(); options.DestinationConditions.TagConditions = DestTagCondition; } Track2Models.CopyFromUriOperation copyId = await destBlob.StartCopyFromUriAsync(srcUri, options, this.CmdletCancellationToken).ConfigureAwait(false); this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destBlob.Name, destBlob.BlobContainerName, copyId)); OutputStream.WriteObject(taskId, new AzureStorageBlob(destBlob, destChannel.StorageContext, properties, options: ClientOptions)); } }
/// <summary> /// Stop copy operation by CloudBlob object /// </summary> /// <param name="blob">CloudBlob object</param> /// <param name="copyId">Copy id</param> private async Task StopCopyBlob(long taskId, IStorageBlobManagement localChannel, CloudBlob blob, string copyId, bool fetchCopyIdFromBlob = false) { ValidateBlobType(blob); if (UseTrack2SDK()) // Use Track2 { if (null == blob) { throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name)); } BlobBaseClient blobBaseClient = AzureStorageBlob.GetTrack2BlobClient(blob, Channel.StorageContext, this.ClientOptions); string specifiedCopyId = copyId; if (string.IsNullOrEmpty(specifiedCopyId) && fetchCopyIdFromBlob) { if (blob.CopyState != null) { specifiedCopyId = blob.CopyState.CopyId; } } string abortCopyId = string.Empty; if (string.IsNullOrEmpty(specifiedCopyId) || Force) { //Make sure we use the correct copy id to abort //Use default retry policy for FetchBlobAttributes Track2Models.BlobProperties blobProperties = blobBaseClient.GetProperties(this.BlobRequestConditions, this.CmdletCancellationToken).Value; if (String.IsNullOrEmpty(blobProperties.CopyId)) { ArgumentException e = new ArgumentException(String.Format(Resources.CopyTaskNotFound, blobBaseClient.Name, blobBaseClient.BlobContainerName)); OutputStream.WriteError(taskId, e); } else { abortCopyId = blobProperties.CopyId; } if (!Force) { string confirmation = String.Format(Resources.ConfirmAbortCopyOperation, blobBaseClient.Name, blobBaseClient.BlobContainerName, abortCopyId); if (!await OutputStream.ConfirmAsync(confirmation).ConfigureAwait(false)) { string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, blobBaseClient.Name, blobBaseClient.BlobContainerName); OutputStream.WriteVerbose(taskId, cancelMessage); } } } else { abortCopyId = specifiedCopyId; } await blobBaseClient.AbortCopyFromUriAsync(abortCopyId, this.BlobRequestConditions, this.CmdletCancellationToken).ConfigureAwait(false); //localChannel.AbortCopyAsync(blob, abortCopyId, accessCondition, abortRequestOption, OperationContext, CmdletCancellationToken).ConfigureAwait(false); string message = String.Format(Resources.StopCopyBlobSuccessfully, blobBaseClient.Name, blobBaseClient.BlobContainerName); OutputStream.WriteObject(taskId, message); } else // use Track1 { AccessCondition accessCondition = null; BlobRequestOptions abortRequestOption = RequestOptions ?? new BlobRequestOptions(); //Set no retry to resolve the 409 conflict exception abortRequestOption.RetryPolicy = new NoRetry(); if (null == blob) { throw new ArgumentException(String.Format(Resources.ObjectCannotBeNull, typeof(CloudBlob).Name)); } string specifiedCopyId = copyId; if (string.IsNullOrEmpty(specifiedCopyId) && fetchCopyIdFromBlob) { if (blob.CopyState != null) { specifiedCopyId = blob.CopyState.CopyId; } } string abortCopyId = string.Empty; if (string.IsNullOrEmpty(specifiedCopyId) || Force) { //Make sure we use the correct copy id to abort //Use default retry policy for FetchBlobAttributes BlobRequestOptions options = RequestOptions; await localChannel.FetchBlobAttributesAsync(blob, accessCondition, options, OperationContext, CmdletCancellationToken).ConfigureAwait(false); if (blob.CopyState == null || String.IsNullOrEmpty(blob.CopyState.CopyId)) { ArgumentException e = new ArgumentException(String.Format(Resources.CopyTaskNotFound, blob.Name, blob.Container.Name)); OutputStream.WriteError(taskId, e); } else { abortCopyId = blob.CopyState.CopyId; } if (!Force) { string confirmation = String.Format(Resources.ConfirmAbortCopyOperation, blob.Name, blob.Container.Name, abortCopyId); if (!await OutputStream.ConfirmAsync(confirmation).ConfigureAwait(false)) { string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, blob.Name, blob.Container.Name); OutputStream.WriteVerbose(taskId, cancelMessage); } } } else { abortCopyId = specifiedCopyId; } await localChannel.AbortCopyAsync(blob, abortCopyId, accessCondition, abortRequestOption, OperationContext, CmdletCancellationToken).ConfigureAwait(false); string message = String.Format(Resources.StopCopyBlobSuccessfully, blob.Name, blob.Container.Name); OutputStream.WriteObject(taskId, message); } }