From 6cf377716f33b2add905eba8c21c565f79cb122a Mon Sep 17 00:00:00 2001 From: Wei Wei Date: Tue, 11 Aug 2020 19:43:32 +0800 Subject: [PATCH] [Storage] Force overwrite Query result file --- .../help/Get-AzStorageBlobQueryResult.md | 21 ++++++++-- .../Cmdlet/GetAzStorageBlobQueryResult.cs | 40 ++++++++++++------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/Storage/Storage.Management/help/Get-AzStorageBlobQueryResult.md b/src/Storage/Storage.Management/help/Get-AzStorageBlobQueryResult.md index f83c567f7277..f675f9295d1d 100644 --- a/src/Storage/Storage.Management/help/Get-AzStorageBlobQueryResult.md +++ b/src/Storage/Storage.Management/help/Get-AzStorageBlobQueryResult.md @@ -17,7 +17,7 @@ Applies a simple Structured Query Language (SQL) statement on a blob's contents Get-AzStorageBlobQueryResult [-Blob] [-Container] [-SnapshotTime ] [-VersionId ] -QueryString -ResultFile [-InputTextConfiguration ] - [-OutputTextConfiguration ] [-PassThru] [-Context ] + [-OutputTextConfiguration ] [-PassThru] [-Force] [-Context ] [-ServerTimeoutPerRequest ] [-ClientTimeoutPerRequest ] [-DefaultProfile ] [-ConcurrentTaskCount ] [-WhatIf] [-Confirm] [] @@ -27,7 +27,7 @@ Get-AzStorageBlobQueryResult [-Blob] [-Container] [-SnapshotTi ``` Get-AzStorageBlobQueryResult -BlobBaseClient -QueryString -ResultFile [-InputTextConfiguration ] - [-OutputTextConfiguration ] [-PassThru] [-Context ] + [-OutputTextConfiguration ] [-PassThru] [-Force] [-Context ] [-ServerTimeoutPerRequest ] [-ClientTimeoutPerRequest ] [-DefaultProfile ] [-ConcurrentTaskCount ] [-WhatIf] [-Confirm] [] @@ -38,7 +38,7 @@ Get-AzStorageBlobQueryResult -BlobBaseClient -QueryString [-Blob] [-SnapshotTime ] [-VersionId ] -QueryString -ResultFile [-InputTextConfiguration ] - [-OutputTextConfiguration ] [-PassThru] [-Context ] + [-OutputTextConfiguration ] [-PassThru] [-Force] [-Context ] [-ServerTimeoutPerRequest ] [-ClientTimeoutPerRequest ] [-DefaultProfile ] [-ConcurrentTaskCount ] [-WhatIf] [-Confirm] [] @@ -220,6 +220,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Force +Force to overwrite the existing file. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -InputTextConfiguration The configuration used to handled the query input text. Create configuration object the with New-AzStorageBlobQueryConfig. diff --git a/src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs b/src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs index 30cb355aa25a..19c784a6ab1b 100644 --- a/src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs +++ b/src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs @@ -27,7 +27,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.Blob using System.Threading.Tasks; using Track2Models = global::Azure.Storage.Blobs.Models; - [Cmdlet("Get", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageBlobQueryResult", DefaultParameterSetName = NameParameterSet, SupportsShouldProcess = true),OutputType(typeof(BlobQueryOutput))] + [Cmdlet("Get", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageBlobQueryResult", DefaultParameterSetName = NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(BlobQueryOutput))] public class GetStorageAzureBlobQueryResultCommand : StorageCloudBlobCmdletBase { /// @@ -46,7 +46,7 @@ public class GetStorageAzureBlobQueryResultCommand : StorageCloudBlobCmdletBase private const string NameParameterSet = "NamePipeline"; private List queryErrors = new List(); - private long bytesScanned = 0; + private long bytesScanned = 0; [Parameter(HelpMessage = "BlobBaseClient Object", Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = BlobPipelineParameterSet)] @@ -105,6 +105,9 @@ public string Container [Parameter(Mandatory = false, HelpMessage = "Return whether the specified blob is successfully queried.")] public SwitchParameter PassThru { get; set; } + [Parameter(HelpMessage = "Force to overwrite the existing file.")] + public SwitchParameter Force { get; set; } + protected override bool UseTrack2Sdk() { return true; @@ -126,7 +129,7 @@ public GetStorageAzureBlobQueryResultCommand(IStorageBlobManagement channel) { Channel = channel; } - + /// /// Cmdlet begin processing @@ -151,11 +154,11 @@ public override void ExecuteCmdlet() case BlobPipelineParameterSet: break; case ContainerPipelineParameterSet: - this.BlobBaseClient = Util.GetTrack2BlobClient(this.BlobContainerClient, - this.Blob, Channel.StorageContext, - this.VersionId, - null, - this.SnapshotTime is null? null : this.SnapshotTime.Value.ToString("o"), + this.BlobBaseClient = Util.GetTrack2BlobClient(this.BlobContainerClient, + this.Blob, Channel.StorageContext, + this.VersionId, + null, + this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"), this.ClientOptions, Track2Models.BlobType.Block); break; case NameParameterSet: @@ -179,11 +182,11 @@ internal async Task QueryAzureBlob(long taskId, IStorageBlobManagement localChan { IProgress progressHandler = new Progress((finishedBytes) => { - bytesScanned = finishedBytes; + bytesScanned = finishedBytes; }); // preapre query Option - // Not show the ProgressHandler now, since the ProgressHandler can't represent the read query progress + // Not show the Progressbar now, since the ProgressHandler can't represent the read query progress Track2Models.BlobQueryOptions queryOption = new Track2Models.BlobQueryOptions { InputTextConfiguration = this.InputTextConfiguration is null ? null : this.InputTextConfiguration.ParseBlobQueryTextConfiguration(), @@ -196,13 +199,20 @@ internal async Task QueryAzureBlob(long taskId, IStorageBlobManagement localChan queryErrors.Add(new PSBlobQueryError(e)); }; - using (var reader = (await ((BlockBlobClient)blob).QueryAsync(query, queryOption, CmdletCancellationToken)).Value.Content) + if (this.Force.IsPresent + || !System.IO.File.Exists(this.ResultFile) + || ShouldContinue(string.Format(Resources.OverwriteConfirmation, this.ResultFile), null)) { - FileStream fs = File.Create(this.ResultFile); - reader.CopyTo(fs); - fs.Close(); + { + using (var reader = (await ((BlockBlobClient)blob).QueryAsync(query, queryOption, CmdletCancellationToken)).Value.Content) + { + FileStream fs = File.Create(this.ResultFile); + reader.CopyTo(fs); + fs.Close(); + } + OutputStream.WriteObject(taskId, new BlobQueryOutput(bytesScanned, queryErrors)); + } } - OutputStream.WriteObject(taskId, new BlobQueryOutput(bytesScanned, queryErrors)); } } }