From 449436954caf12e09d858f669c8fc002e6c56a11 Mon Sep 17 00:00:00 2001 From: yifanz0 <100746763+yifanz0@users.noreply.github.com> Date: Mon, 20 Mar 2023 19:20:22 +0800 Subject: [PATCH] [Storage] Fix file cmdlet context issue when current context doesn't match Track1 object context from input (#21228) * Fix file cmdlet context issue * Update changelog message and fix context * Create ChangeLog.md --------- Co-authored-by: Yabo Hu --- src/Storage/Storage.Management/ChangeLog.md | 13 ++++++ .../File/AzureStorageFileCmdletBase.cs | 40 ++++++++++++++----- .../Cmdlet/CloseAzureStorageFileHandle.cs | 18 --------- .../File/Cmdlet/GetAzureStorageFile.cs | 10 +++-- .../File/Cmdlet/GetAzureStorageFileContent.cs | 20 +++++++++- .../File/Cmdlet/GetAzureStorageFileHandle.cs | 19 +-------- .../File/Cmdlet/NewAzureStorageDirectory.cs | 10 +++-- .../Cmdlet/NewAzureStorageFileSasToken.cs | 5 ++- .../Cmdlet/RemoveAzureStorageDirectory.cs | 10 +++-- .../File/Cmdlet/RemoveAzureStorageFile.cs | 15 ++++--- .../File/Cmdlet/RemoveAzureStorageShare.cs | 5 ++- .../File/Cmdlet/SetAzureStorageFileContent.cs | 14 ++++++- .../File/Cmdlet/SetAzureStorageShareQuota.cs | 5 ++- .../File/Cmdlet/StartAzureStorageFileCopy.cs | 10 +++-- 14 files changed, 118 insertions(+), 76 deletions(-) diff --git a/src/Storage/Storage.Management/ChangeLog.md b/src/Storage/Storage.Management/ChangeLog.md index ce204134c95b..cd5dff681dbf 100644 --- a/src/Storage/Storage.Management/ChangeLog.md +++ b/src/Storage/Storage.Management/ChangeLog.md @@ -18,6 +18,19 @@ - Additional information about change #1 --> ## Upcoming Release +* Fixed file cmdlets potential context issue when the current context doesn't match with the credential of input Azure File object + - `Close-AzStorageFileHandle` + - `Get-AzStorageFile` + - `Get-AzStorageFileContent` + - `Get-AzStorageFileHandle` + - `New-AzStorageDirectory` + - `New-AzStorageFileSASToken` + - `Remove-AzStorageDirectory` + - `Remove-AzStorageFile` + - `Remove-AzStorageShare` + - `Set-AzStorageFileContent` + - `Set-AzStorageShareQuota` + - `Start-AzStorageFileCopy` ## Version 5.4.1 * Updated Azure.Core to 1.28.0. diff --git a/src/Storage/Storage/File/AzureStorageFileCmdletBase.cs b/src/Storage/Storage/File/AzureStorageFileCmdletBase.cs index 5533a13b036b..f1f37fa9bffa 100644 --- a/src/Storage/Storage/File/AzureStorageFileCmdletBase.cs +++ b/src/Storage/Storage/File/AzureStorageFileCmdletBase.cs @@ -31,6 +31,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.File using System.Linq; using Microsoft.Azure.Cosmos.Table; using Microsoft.Azure.Storage.Blob; + using Microsoft.WindowsAzure.Commands.Storage.Adapters; public abstract class AzureStorageFileCmdletBase : StorageCloudCmdletBase { @@ -95,15 +96,34 @@ protected bool ShareIsEmpty(ShareClient share) } } + protected bool ShouldSetContext(IStorageContext context, CloudFileClient cloudFileClient) + { + if (context == null) + { + return true; + } + try + { + if (context.GetCloudStorageAccount().FileEndpoint.Host.Equals(cloudFileClient.BaseUri.Host, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + } catch (Exception) + { + return true; + } + return true; + } + /// /// Write CloudFile to output using specified service channel /// /// Task id - /// IStorageFileManagement channel object + /// AzureStorageContext object /// The output CloudFile object - internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel, CloudFile file) + internal void WriteCloudFileObject(long taskId, AzureStorageContext context, CloudFile file) { - AzureStorageFile azureFile = new AzureStorageFile(file, channel.StorageContext); + AzureStorageFile azureFile = new AzureStorageFile(file, context); OutputStream.WriteObject(taskId, azureFile); } @@ -112,11 +132,11 @@ internal void WriteCloudFileObject(long taskId, IStorageFileManagement channel, /// Write CloudFileDirectory to output using specified service channel /// /// Task id - /// IStorageFileManagement channel object + /// AzureStorageContext object /// The output CloudFileDirectory object - internal void WriteCloudFileDirectoryeObject(long taskId, IStorageFileManagement channel, CloudFileDirectory fileDir) + internal void WriteCloudFileDirectoryeObject(long taskId, AzureStorageContext context, CloudFileDirectory fileDir) { - AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, channel.StorageContext); + AzureStorageFileDirectory azureFileDir = new AzureStorageFileDirectory(fileDir, context); OutputStream.WriteObject(taskId, azureFileDir); } @@ -136,17 +156,17 @@ internal void WriteCloudShareObject(long taskId, IStorageFileManagement channel, /// Write IListFileItem to output using specified service channel /// /// Task id - /// IStorageFileManagement channel object + /// AzureStorageContext object /// The output IListFileItem object - internal void WriteListFileItemObject(long taskId, IStorageFileManagement channel, IListFileItem item) + internal void WriteListFileItemObject(long taskId, AzureStorageContext context, IListFileItem item) { if ((item as CloudFile) != null) // CloudFile { - WriteCloudFileObject(taskId, channel, item as CloudFile); + WriteCloudFileObject(taskId, context, item as CloudFile); } else { - WriteCloudFileDirectoryeObject(taskId, channel, item as CloudFileDirectory); + WriteCloudFileDirectoryeObject(taskId, context, item as CloudFileDirectory); } } diff --git a/src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs b/src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs index 2213bb67a140..67f1816a3102 100644 --- a/src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs +++ b/src/Storage/Storage/File/Cmdlet/CloseAzureStorageFileHandle.cs @@ -184,12 +184,6 @@ public override void ExecuteCmdlet() { case DirectoryCloseAllParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); - } break; case ShareNameCloseSingleParameterSetName: @@ -202,22 +196,10 @@ public override void ExecuteCmdlet() case ShareCloseSingleParameterSetName: case ShareCloseAllParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); - } break; case FileCloseAllParameterSetName: targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext); - } break; default: diff --git a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs index 4a3f915bb6ca..1263f3cd13cd 100644 --- a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs +++ b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFile.cs @@ -75,8 +75,9 @@ public override void ExecuteCmdlet() case Constants.DirectoryParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); } @@ -91,8 +92,9 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs index e46bbcef0922..9736d183f426 100644 --- a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs +++ b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileContent.cs @@ -155,6 +155,12 @@ public override void ExecuteCmdlet() { case LocalConstants.FileParameterSetName: fileToBeDownloaded = this.File; + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.File.ServiceClient)) + { + this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext); + } break; case LocalConstants.ShareNameParameterSetName: @@ -164,10 +170,22 @@ public override void ExecuteCmdlet() case LocalConstants.ShareParameterSetName: fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path); + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) + { + this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); + } break; case LocalConstants.DirectoryParameterSetName: fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path); + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) + { + this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); + } break; default: @@ -264,7 +282,7 @@ await DataMovementTransferHelper.DoTransfer(() => if (this.PassThru) { - WriteCloudFileObject(taskId, this.Channel, fileToBeDownloaded); + WriteCloudFileObject(taskId, (AzureStorageContext)this.Context, fileToBeDownloaded); } }); } diff --git a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs index 7c1f3a4323a8..730bf5939aee 100644 --- a/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs +++ b/src/Storage/Storage/File/Cmdlet/GetAzureStorageFileHandle.cs @@ -98,12 +98,6 @@ public override void ExecuteCmdlet() { case Constants.DirectoryParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); - } break; case Constants.ShareNameParameterSetName: @@ -114,21 +108,10 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); - } break; + case Constants.FileParameterSetName: targetFile = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions); - - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) - { - this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext); - } break; default: diff --git a/src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs b/src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs index 508a3808f75c..55f80e1d126f 100644 --- a/src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs +++ b/src/Storage/Storage/File/Cmdlet/NewAzureStorageDirectory.cs @@ -72,8 +72,9 @@ public override void ExecuteCmdlet() case Constants.DirectoryParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); } @@ -88,8 +89,9 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/NewAzureStorageFileSasToken.cs b/src/Storage/Storage/File/Cmdlet/NewAzureStorageFileSasToken.cs index 76e01ebe4c80..567d3655c902 100644 --- a/src/Storage/Storage/File/Cmdlet/NewAzureStorageFileSasToken.cs +++ b/src/Storage/Storage/File/Cmdlet/NewAzureStorageFileSasToken.cs @@ -159,8 +159,9 @@ public override void ExecuteCmdlet() ShareFileClient fileClient; if (null != this.File) { - // when only track1 object input, might miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.File.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs index a26db2b3333f..d4025b42ebf7 100644 --- a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs +++ b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageDirectory.cs @@ -83,8 +83,9 @@ public override void ExecuteCmdlet() case Constants.DirectoryParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); } @@ -100,8 +101,9 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: baseDirClient = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageFile.cs b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageFile.cs index ce2a5fba4afc..fd1f412a9d13 100644 --- a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageFile.cs +++ b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageFile.cs @@ -95,8 +95,9 @@ public override void ExecuteCmdlet() case Constants.FileParameterSetName: fileToBeRemoved = AzureStorageFile.GetTrack2FileClient(this.File, ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.File.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.File.ServiceClient, DefaultContext); } @@ -111,8 +112,9 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: fileToBeRemoved = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Share.GetRootDirectoryReference(), ClientOptions).GetFileClient(this.Path); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } @@ -121,8 +123,9 @@ public override void ExecuteCmdlet() case Constants.DirectoryParameterSetName: fileToBeRemoved = AzureStorageFileDirectory.GetTrack2FileDirClient(this.Directory, ClientOptions).GetFileClient(this.Path); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageShare.cs b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageShare.cs index 338de1d5410e..cdb150fc2823 100644 --- a/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageShare.cs +++ b/src/Storage/Storage/File/Cmdlet/RemoveAzureStorageShare.cs @@ -105,8 +105,9 @@ public override void ExecuteCmdlet() share = AzureStorageFileShare.GetTrack2FileShareClient(this.Share, (AzureStorageContext)this.Context, this.ClientOptions); } - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs b/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs index a6a2e7da918e..ec031d32778f 100644 --- a/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs +++ b/src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs @@ -284,7 +284,7 @@ await DataMovementTransferHelper.DoTransfer(() => if (this.PassThru) { - WriteCloudFileObject(taskId, this.Channel, cloudFileToBeUploaded); + WriteCloudFileObject(taskId, (AzureStorageContext)this.Context, cloudFileToBeUploaded); } }); } @@ -314,6 +314,12 @@ private async Task BuildCloudFileInstanceFromPathAsync(string default { case LocalConstants.DirectoryParameterSetName: baseDirectory = this.Directory; + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Directory.ServiceClient)) + { + this.Context = GetStorageContextFromTrack1FileServiceClient(this.Directory.ServiceClient, DefaultContext); + } break; case LocalConstants.ShareNameParameterSetName: @@ -323,6 +329,12 @@ private async Task BuildCloudFileInstanceFromPathAsync(string default case LocalConstants.ShareParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) + { + this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); + } break; default: diff --git a/src/Storage/Storage/File/Cmdlet/SetAzureStorageShareQuota.cs b/src/Storage/Storage/File/Cmdlet/SetAzureStorageShareQuota.cs index a43c4b8628cc..a6b238348b08 100644 --- a/src/Storage/Storage/File/Cmdlet/SetAzureStorageShareQuota.cs +++ b/src/Storage/Storage/File/Cmdlet/SetAzureStorageShareQuota.cs @@ -73,8 +73,9 @@ public override void ExecuteCmdlet() case Constants.ShareParameterSetName: share = AzureStorageFileShare.GetTrack2FileShareClient(this.Share, (AzureStorageContext)this.Context, this.ClientOptions); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.Share.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.Share.ServiceClient, DefaultContext); } diff --git a/src/Storage/Storage/File/Cmdlet/StartAzureStorageFileCopy.cs b/src/Storage/Storage/File/Cmdlet/StartAzureStorageFileCopy.cs index ff52efa3d3a5..d835dbe3ce82 100644 --- a/src/Storage/Storage/File/Cmdlet/StartAzureStorageFileCopy.cs +++ b/src/Storage/Storage/File/Cmdlet/StartAzureStorageFileCopy.cs @@ -251,8 +251,9 @@ public override void ExecuteCmdlet() { if(this.DestFile != null) { - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.DestContext == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.DestContext, this.DestFile.ServiceClient)) { this.DestContext = GetStorageContextFromTrack1FileServiceClient(this.DestFile.ServiceClient, DefaultContext); } @@ -338,8 +339,9 @@ private void StartCopyFromFile() sourceFile = this.SrcFile; filePath = this.SrcFile.GetFullPath(); - // when only track1 object input, will miss storage context, so need to build storage context for prepare the output object. - if (this.Context == null) + // Build and set storage context for the output object when + // 1. input track1 object and storage context is missing 2. the current context doesn't match the context of the input object + if (ShouldSetContext(this.Context, this.SrcFile.ServiceClient)) { this.Context = GetStorageContextFromTrack1FileServiceClient(this.SrcFile.ServiceClient, DefaultContext); }