Skip to content

Commit e48b96f

Browse files
authored
[Storage] Support find blob by tag in a container. (#19831)
1 parent f296d27 commit e48b96f

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
-->
2020
## Upcoming Release
2121
* Supported create/upgrade storage account with Keyvault from another tenant and access Keyvault with FederatedClientId
22-
- `New-AzStorageAccount`
23-
- `Set-AzStorageAccount`
22+
* `New-AzStorageAccount`
23+
* `Set-AzStorageAccount`
24+
* Supported find blobs in a container with a blob tag filter sql expression
25+
* `Get-AzStorageBlobByTag`
2426

2527
## Version 4.10.0
2628
* Migrated following Azure File dataplane cmdlets from 'Microsoft.Azure.Storage.File 11.2.2' to 'Azure.Storage.Files.Shares 12.10.0'

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Lists blobs in a storage account across containers, with a blob tag filter sql e
1414

1515
```
1616
Get-AzStorageBlobByTag -TagFilterSqlExpression <String> [-MaxCount <Int32>]
17-
[-ContinuationToken <BlobContinuationToken>] [-GetBlobProperty] [-Context <IStorageContext>]
18-
[-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
17+
[-ContinuationToken <BlobContinuationToken>] [-GetBlobProperty] [-Container <String>]
18+
[-Context <IStorageContext>] [-ServerTimeoutPerRequest <Int32>] [-ClientTimeoutPerRequest <Int32>]
1919
[-DefaultProfile <IAzureContextContainer>] [-ConcurrentTaskCount <Int32>] [<CommonParameters>]
2020
```
2121

@@ -26,6 +26,8 @@ The **Get-AzStorageBlobByTag** cmdlet lists blobs in a storage account across co
2626

2727
### Example 1: List all blobs match a specific blob tag, across containers.
2828
<!-- Skip: Output cannot be splitted from code -->
29+
30+
2931
```
3032
PS C:\> Get-AzStorageBlobByTag -TagFilterSqlExpression """tag1""='value1'" -Context $ctx
3133
@@ -44,12 +46,14 @@ testblob3
4446
testblob4 False
4547
```
4648

47-
This command lists all blobs in a storage accoun, which contains a tag with name "tag1" and value "value1".
49+
This command lists all blobs in a storage account, which contains a tag with name "tag1" and value "value1".
4850

4951
### Example 2: List blobs in a specific container and match a specific blob tag
5052
<!-- Skip: Output cannot be splitted from code -->
53+
54+
5155
```
52-
PS C:\> Get-AzStorageBlobByTag -TagFilterSqlExpression "@container='containername' AND ""tag1""='value1'" -Context $ctx
56+
PS C:\> Get-AzStorageBlobByTag -Container 'containername' -TagFilterSqlExpression """tag1""='value1'" -Context $ctx
5357
5458
AccountName: storageaccountname, ContainerName: containername
5559
@@ -63,6 +67,8 @@ This command lists blobs in a container and match a specific blob tag.
6367

6468
### Example 3: List all blobs match a specific blob tag, across containers, and get the blob properties.
6569
<!-- Skip: Output cannot be splitted from code -->
70+
71+
6672
```
6773
PS C:\> Get-AzStorageBlobByTag -TagFilterSqlExpression """tag1""='value1'" -GetBlobProperty
6874
@@ -81,7 +87,7 @@ testblob3 BlockBlob 100 application/octet-stream 20
8187
testblob4 BlockBlob 2024 application/octet-stream 2020-07-01 09:42:11Z Hot False 2020-07-01T09:42:11.4283807Z *
8288
```
8389

84-
This command lists all blobs in a storage accoun, which contains a tag with name "tag1" and value "value1", and get the blob properties.
90+
This command lists all blobs in a storage account, which contains a tag with name "tag1" and value "value1", and get the blob properties.
8591
Please note, to get blob properties with parameter -GetBlobProperty, each blob will need an addtional request, so the cmdlet runs show when there are many blobs.
8692

8793
## PARAMETERS
@@ -117,6 +123,21 @@ Accept pipeline input: False
117123
Accept wildcard characters: False
118124
```
119125
126+
### -Container
127+
Container name, specify this parameter to only return all blobs whose tags match a search expression in the container.
128+
129+
```yaml
130+
Type: System.String
131+
Parameter Sets: (All)
132+
Aliases:
133+
134+
Required: False
135+
Position: Named
136+
Default value: None
137+
Accept pipeline input: False
138+
Accept wildcard characters: False
139+
```
140+
120141
### -Context
121142
Azure Storage Context Object
122143

src/Storage/Storage/Blob/Cmdlet/GetAzureStorageBlobByTag.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public int? MaxCount
6363
[Parameter(Mandatory = false, HelpMessage = "As the blobs get by tag don't contain blob proeprties, specify tis parameter to get blob properties with an additional request on each blob.")]
6464
public SwitchParameter GetBlobProperty { get; set; }
6565

66+
[Parameter(Mandatory = false, HelpMessage = "Container name, specify this parameter to only return all blobs whose tags match a search expression in the container.")]
67+
[ValidateNotNullOrEmpty]
68+
public string Container { get; set; }
69+
6670
// Overwrite the useless parameter
6771
public override string TagCondition { get; set; }
6872
protected override bool UseTrack2Sdk()
@@ -94,6 +98,12 @@ internal void ListBlobsByTag(IStorageBlobManagement localChannel, string tagFilt
9498
{
9599

96100
BlobServiceClient blobServiceClient = Util.GetTrack2BlobServiceClient(localChannel.StorageContext, ClientOptions);
101+
BlobContainerClient containerClient = null;
102+
if (!string.IsNullOrEmpty(this.Container))
103+
{
104+
containerClient = blobServiceClient.GetBlobContainerClient(this.Container);
105+
}
106+
97107

98108
int listCount = InternalMaxCount;
99109
int MaxListCount = 5000;
@@ -106,9 +116,20 @@ internal void ListBlobsByTag(IStorageBlobManagement localChannel, string tagFilt
106116
{
107117
requestCount = Math.Min(listCount, MaxListCount);
108118
realListCount = 0;
109-
IEnumerator<Page<TaggedBlobItem>> enumerator = blobServiceClient.FindBlobsByTags(tagFilterSqlExpression, CmdletCancellationToken)
110-
.AsPages(track2ContinuationToken, requestCount)
111-
.GetEnumerator();
119+
IEnumerator<Page<TaggedBlobItem>> enumerator;
120+
121+
if (string.IsNullOrEmpty(this.Container))
122+
{
123+
enumerator = blobServiceClient.FindBlobsByTags(tagFilterSqlExpression, CmdletCancellationToken)
124+
.AsPages(track2ContinuationToken, requestCount)
125+
.GetEnumerator();
126+
}
127+
else
128+
{
129+
enumerator = containerClient.FindBlobsByTags(tagFilterSqlExpression, CmdletCancellationToken)
130+
.AsPages(track2ContinuationToken, requestCount)
131+
.GetEnumerator();
132+
}
112133

113134
Page<TaggedBlobItem> page;
114135
enumerator.MoveNext();

0 commit comments

Comments
 (0)