Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] Force overwrite Query result file #12639

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Applies a simple Structured Query Language (SQL) statement on a blob's contents
Get-AzStorageBlobQueryResult [-Blob] <String> [-Container] <String> [-SnapshotTime <DateTimeOffset>]
[-VersionId <String>] -QueryString <String> -ResultFile <String>
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
[<CommonParameters>]
Expand All @@ -27,7 +27,7 @@ Get-AzStorageBlobQueryResult [-Blob] <String> [-Container] <String> [-SnapshotTi
```
Get-AzStorageBlobQueryResult -BlobBaseClient <BlobBaseClient> -QueryString <String> -ResultFile <String>
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
[<CommonParameters>]
Expand All @@ -38,7 +38,7 @@ Get-AzStorageBlobQueryResult -BlobBaseClient <BlobBaseClient> -QueryString <Stri
Get-AzStorageBlobQueryResult -BlobContainerClient <BlobContainerClient> [-Blob] <String>
[-SnapshotTime <DateTimeOffset>] [-VersionId <String>] -QueryString <String> -ResultFile <String>
[-InputTextConfiguration <PSBlobQueryTextConfiguration>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Context <IStorageContext>]
[-OutputTextConfiguration <PSBlobQueryTextConfiguration>] [-PassThru] [-Force] [-Context <IStorageContext>]
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [-WhatIf] [-Confirm]
[<CommonParameters>]
Expand Down Expand Up @@ -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.

Expand Down
40 changes: 25 additions & 15 deletions src/Storage/Storage/Blob/Cmdlet/GetAzStorageBlobQueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
Expand All @@ -46,7 +46,7 @@ public class GetStorageAzureBlobQueryResultCommand : StorageCloudBlobCmdletBase
private const string NameParameterSet = "NamePipeline";

private List<PSBlobQueryError> queryErrors = new List<PSBlobQueryError>();
private long bytesScanned = 0;
private long bytesScanned = 0;

[Parameter(HelpMessage = "BlobBaseClient Object", Mandatory = true,
ValueFromPipelineByPropertyName = true, ParameterSetName = BlobPipelineParameterSet)]
Expand Down Expand Up @@ -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;
Expand All @@ -126,7 +129,7 @@ public GetStorageAzureBlobQueryResultCommand(IStorageBlobManagement channel)
{
Channel = channel;
}


/// <summary>
/// Cmdlet begin processing
Expand All @@ -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:
Expand All @@ -179,11 +182,11 @@ internal async Task QueryAzureBlob(long taskId, IStorageBlobManagement localChan
{
IProgress<long> progressHandler = new Progress<long>((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(),
Expand All @@ -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));
}
}
}