Skip to content

Commit e9aed41

Browse files
authored
[Storage] Force overwrite Query result file (#12639)
1 parent 1bbc20b commit e9aed41

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/Storage/Storage.Management/help/Get-AzStorageBlobQueryResult.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Applies a simple Structured Query Language (SQL) statement on a blob's contents
1717
Get-AzStorageBlobQueryResult [-Blob] <String> [-Container] <String> [-SnapshotTime <DateTimeOffset>]
1818
[-VersionId <String>] -QueryString <String> -ResultFile <String>
1919
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
20-
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
20+
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
2121
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
2222
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
2323
[<CommonParameters>]
@@ -27,7 +27,7 @@ Get-AzStorageBlobQueryResult [-Blob] <String> [-Container] <String> [-SnapshotTi
2727
```
2828
Get-AzStorageBlobQueryResult -BlobBaseClient <BlobBaseClient> -QueryString <String> -ResultFile <String>
2929
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
30-
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
30+
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
3131
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
3232
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
3333
[<CommonParameters>]
@@ -38,7 +38,7 @@ Get-AzStorageBlobQueryResult -BlobBaseClient <BlobBaseClient> -QueryString <Stri
3838
Get-AzStorageBlobQueryResult -BlobContainerClient <BlobContainerClient> [-Blob] <String>
3939
[-SnapshotTime <DateTimeOffset>] [-VersionId <String>] -QueryString <String> -ResultFile <String>
4040
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
41-
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
41+
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
4242
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
4343
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
4444
[<CommonParameters>]
@@ -220,6 +220,21 @@ Accept pipeline input: False
220220
Accept wildcard characters: False
221221
```
222222
223+
### -Force
224+
Force to overwrite the existing file.
225+
226+
```yaml
227+
Type: System.Management.Automation.SwitchParameter
228+
Parameter Sets: (All)
229+
Aliases:
230+
231+
Required: False
232+
Position: Named
233+
Default value: None
234+
Accept pipeline input: False
235+
Accept wildcard characters: False
236+
```
237+
223238
### -InputTextConfiguration
224239
The configuration used to handled the query input text. Create configuration object the with New-AzStorageBlobQueryConfig.
225240

src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.Blob
2727
using System.Threading.Tasks;
2828
using Track2Models = global::Azure.Storage.Blobs.Models;
2929

30-
[Cmdlet("Get", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageBlobQueryResult", DefaultParameterSetName = NameParameterSet, SupportsShouldProcess = true),OutputType(typeof(BlobQueryOutput))]
30+
[Cmdlet("Get", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageBlobQueryResult", DefaultParameterSetName = NameParameterSet, SupportsShouldProcess = true), OutputType(typeof(BlobQueryOutput))]
3131
public class GetStorageAzureBlobQueryResultCommand : StorageCloudBlobCmdletBase
3232
{
3333
/// <summary>
@@ -46,7 +46,7 @@ public class GetStorageAzureBlobQueryResultCommand : StorageCloudBlobCmdletBase
4646
private const string NameParameterSet = "NamePipeline";
4747

4848
private List<PSBlobQueryError> queryErrors = new List<PSBlobQueryError>();
49-
private long bytesScanned = 0;
49+
private long bytesScanned = 0;
5050

5151
[Parameter(HelpMessage = "BlobBaseClient Object", Mandatory = true,
5252
ValueFromPipelineByPropertyName = true, ParameterSetName = BlobPipelineParameterSet)]
@@ -105,6 +105,9 @@ public string Container
105105
[Parameter(Mandatory = false, HelpMessage = "Return whether the specified blob is successfully queried.")]
106106
public SwitchParameter PassThru { get; set; }
107107

108+
[Parameter(HelpMessage = "Force to overwrite the existing file.")]
109+
public SwitchParameter Force { get; set; }
110+
108111
protected override bool UseTrack2Sdk()
109112
{
110113
return true;
@@ -126,7 +129,7 @@ public GetStorageAzureBlobQueryResultCommand(IStorageBlobManagement channel)
126129
{
127130
Channel = channel;
128131
}
129-
132+
130133

131134
/// <summary>
132135
/// Cmdlet begin processing
@@ -151,11 +154,11 @@ public override void ExecuteCmdlet()
151154
case BlobPipelineParameterSet:
152155
break;
153156
case ContainerPipelineParameterSet:
154-
this.BlobBaseClient = Util.GetTrack2BlobClient(this.BlobContainerClient,
155-
this.Blob, Channel.StorageContext,
156-
this.VersionId,
157-
null,
158-
this.SnapshotTime is null? null : this.SnapshotTime.Value.ToString("o"),
157+
this.BlobBaseClient = Util.GetTrack2BlobClient(this.BlobContainerClient,
158+
this.Blob, Channel.StorageContext,
159+
this.VersionId,
160+
null,
161+
this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToString("o"),
159162
this.ClientOptions, Track2Models.BlobType.Block);
160163
break;
161164
case NameParameterSet:
@@ -179,11 +182,11 @@ internal async Task QueryAzureBlob(long taskId, IStorageBlobManagement localChan
179182
{
180183
IProgress<long> progressHandler = new Progress<long>((finishedBytes) =>
181184
{
182-
bytesScanned = finishedBytes;
185+
bytesScanned = finishedBytes;
183186
});
184187

185188
// preapre query Option
186-
// Not show the ProgressHandler now, since the ProgressHandler can't represent the read query progress
189+
// Not show the Progressbar now, since the ProgressHandler can't represent the read query progress
187190
Track2Models.BlobQueryOptions queryOption = new Track2Models.BlobQueryOptions
188191
{
189192
InputTextConfiguration = this.InputTextConfiguration is null ? null : this.InputTextConfiguration.ParseBlobQueryTextConfiguration(),
@@ -196,13 +199,20 @@ internal async Task QueryAzureBlob(long taskId, IStorageBlobManagement localChan
196199
queryErrors.Add(new PSBlobQueryError(e));
197200
};
198201

199-
using (var reader = (await ((BlockBlobClient)blob).QueryAsync(query, queryOption, CmdletCancellationToken)).Value.Content)
202+
if (this.Force.IsPresent
203+
|| !System.IO.File.Exists(this.ResultFile)
204+
|| ShouldContinue(string.Format(Resources.OverwriteConfirmation, this.ResultFile), null))
200205
{
201-
FileStream fs = File.Create(this.ResultFile);
202-
reader.CopyTo(fs);
203-
fs.Close();
206+
{
207+
using (var reader = (await ((BlockBlobClient)blob).QueryAsync(query, queryOption, CmdletCancellationToken)).Value.Content)
208+
{
209+
FileStream fs = File.Create(this.ResultFile);
210+
reader.CopyTo(fs);
211+
fs.Close();
212+
}
213+
OutputStream.WriteObject(taskId, new BlobQueryOutput(bytesScanned, queryErrors));
214+
}
204215
}
205-
OutputStream.WriteObject(taskId, new BlobQueryOutput(bytesScanned, queryErrors));
206216
}
207217
}
208218
}

0 commit comments

Comments
 (0)