Skip to content

Commit 9b0a30b

Browse files
authored
[Storage] Remove track1 SDK references in Get-AzStorageBlob (Azure#23955)
* Remove track1 references in Get-AzStorageBlob * Add live tests; update changelog
1 parent 1abbafc commit 9b0a30b

File tree

3 files changed

+88
-117
lines changed

3 files changed

+88
-117
lines changed

src/Storage/Storage.Management.Test/LiveTests/BlobTests.ps1

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
1313
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowBlobPublicAccess $true
1414
$ctx = $account.Context
1515
$container = New-AzStorageContainer -Name $containerName -Context $ctx
16+
$containerSAS = New-AzStorageContainerSASToken -Name $containerName -Permission radwl -ExpiryTime 5000-01-01 -Context $ctx
17+
$sasCtx = New-AzStorageContext -SasToken $containerSAS -StorageAccountName $storageAccountName
1618

1719
# container ACL and stored access policy
1820
$accessPolicyName = "policy1"
@@ -27,6 +29,7 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
2729
# upload
2830
$blobName = New-LiveTestResourceName
2931
$blobName2 = New-LiveTestResourceName
32+
$blobName3 = "blob3" + $blobName
3033
$b = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName -StandardBlobTier Cool -Properties @{"ContentType" = $ContentType; "ContentMD5" = $ContentMD5} -Context $ctx -Force
3134
Assert-AreEqual $blobName $b.Name
3235
Assert-AreEqual 512 $b.Length
@@ -38,17 +41,49 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
3841
Assert-AreEqual $ContentType $blobs[0].BlobProperties.ContentType
3942
Assert-AreEqual "Cool" $blobs[0].BlobProperties.AccessTier
4043

44+
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName -Context $sasCtx
45+
Assert-AreEqual $blobName $blob.Name
46+
Assert-AreEqual 512 $blob.Length
47+
Assert-AreEqual $ContentType $blob.BlobProperties.ContentType
48+
4149
$b.BlobBaseClient.SetAccessTier("Hot")
4250
$b.FetchAttributes()
4351
Assert-AreEqual "Hot" $b.BlobProperties.AccessTier
4452

4553
$b2 = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName2 -Force -Context $ctx
4654
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
4755
Assert-AreEqual 2 $blobs.Count
56+
$b3 = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Force -Context $sasCtx
57+
$b2.BlobBaseClient.CreateSnapshot()
58+
59+
$blobs = Get-AzStorageBlob -Container $containerName -Prefix "blob3" -Context $ctx
60+
Assert-AreEqual 1 $blobs.Count
61+
Assert-AreEqual $blobName3 $blobs[0].Name
62+
63+
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
64+
$snapshotblob = $blobs | ?{$_.SnapshotTime -ne $null} | Select-Object -First 1
65+
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName2 -SnapshotTime $snapshotblob.SnapshotTime -Context $ctx
66+
Assert-AreEqual $blobName2 $blob.Name
67+
Assert-AreEqual $snapshotblob.SnapshotTime $blob.SnapshotTime
68+
69+
$blob = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Tag @{"Tag1"="Value2";"Tag2"="Value2"} -Context $ctx -Force
70+
$blob = Get-AzStorageBlob -Blob $blobName3 -Container $containerName -IncludeTag -Context $ctx
71+
Assert-AreEqual $blobName3 $blob.Name
72+
Assert-AreEqual 2 $blob.TagCount
73+
Assert-AreEqual 2 $blob.Tags.Count
74+
75+
Update-AzStorageBlobServiceProperty -ResourceGroupName $rgName -StorageAccountName $storageAccountName -IsVersioningEnabled $true
76+
sleep 30
77+
$blob = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Context $sasctx -Force
78+
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx -IncludeVersion
79+
$versionBlob = $blobs | ?{$_.VersionId -ne $null} | Select-Object -First 1
80+
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName3 -VersionId $versionBlob.VersionId -Context $ctx
81+
Assert-AreEqual $blobName3 $blob.Name
82+
Assert-AreEqual $versionBlob.VersionId $blob.VersionId
4883

4984
$b2 | Remove-AzStorageBlob -Force
5085
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
51-
Assert-AreEqual 1 $blobs.Count
86+
Assert-AreEqual 2 $blobs.Count
5287

5388
$container = Get-AzStorageContainer -Name $containerName -Context $ctx
5489
$containerProperties = $container.BlobContainerClient.GetProperties().Value

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Removed some code branches referencing Microsoft.Azure.Storage.Blob
22+
- `Get-AzStorageBlob`
2123
* Updated the prompt message when deleting a share snapshot and the output format when listing
2224
- `Remove-AzStorageShare`
2325
- `Remove-AzRmStorageSahre`

src/Storage/Storage/Blob/Cmdlet/GetAzureStorageBlob.cs

Lines changed: 50 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ public int? MaxCount
166166
[ValidateNotNullOrEmpty]
167167
public override string TagCondition { get; set; }
168168

169-
protected override bool UseTrack2Sdk()
170-
{
171-
return true;
172-
}
173-
174169
/// <summary>
175170
/// Initializes a new instance of the GetAzureStorageBlobCommand class.
176171
/// </summary>
@@ -202,7 +197,6 @@ internal async Task ListBlobsByName(long taskId, IStorageBlobManagement localCha
202197
{
203198
CloudBlobContainer container = null;
204199
BlobRequestOptions requestOptions = RequestOptions;
205-
AccessCondition accessCondition = null;
206200

207201
string prefix = string.Empty;
208202

@@ -232,45 +226,30 @@ internal async Task ListBlobsByName(long taskId, IStorageBlobManagement localCha
232226
}
233227

234228
BlobBaseClient blobClient = null;
235-
if (UseTrack2Sdk()) // User Track2 SDK
236-
{
237-
blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, false, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToUniversalTime().ToString("o").Replace("+00:00", "Z"), ClientOptions, shouldTrimSlash: false);
238-
global::Azure.Storage.Blobs.Models.BlobProperties blobProperties;
239-
try
240-
{
241-
blobProperties = blobClient.GetProperties(BlobRequestConditions, cancellationToken: CmdletCancellationToken);
242-
}
243-
catch (global::Azure.RequestFailedException e) when (e.Status == 404)
244-
{
245-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
246-
}
247-
blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, blobProperties.IsLatestVersion, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToUniversalTime().ToString("o").Replace("+00:00", "Z"), ClientOptions, blobProperties.BlobType, shouldTrimSlash: false);
248229

249-
AzureStorageBlob outputBlob = new AzureStorageBlob(blobClient, localChannel.StorageContext, blobProperties, ClientOptions);
250-
if (this.IncludeTag.IsPresent)
251-
{
252-
IDictionary<string, string> tags = (await blobClient.GetTagsAsync(null, this.CmdletCancellationToken).ConfigureAwait(false)).Value.Tags;
253-
if (tags != null)
254-
{
255-
outputBlob.Tags = tags.ToHashtable();
256-
outputBlob.TagCount = tags.Count;
257-
}
258-
}
259-
OutputStream.WriteObject(taskId, outputBlob);
230+
blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, false, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToUniversalTime().ToString("o").Replace("+00:00", "Z"), ClientOptions, shouldTrimSlash: false);
231+
global::Azure.Storage.Blobs.Models.BlobProperties blobProperties;
232+
try
233+
{
234+
blobProperties = blobClient.GetProperties(BlobRequestConditions, cancellationToken: CmdletCancellationToken);
260235
}
261-
else // Use Track1 SDK
236+
catch (global::Azure.RequestFailedException e) when (e.Status == 404)
262237
{
263-
CloudBlob blob = await localChannel.GetBlobReferenceFromServerAsync(container, blobName, this.SnapshotTime, accessCondition, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);
238+
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
239+
}
240+
blobClient = Util.GetTrack2BlobClient(track2container, blobName, localChannel.StorageContext, this.VersionId, blobProperties.IsLatestVersion, this.SnapshotTime is null ? null : this.SnapshotTime.Value.ToUniversalTime().ToString("o").Replace("+00:00", "Z"), ClientOptions, blobProperties.BlobType, shouldTrimSlash: false);
264241

265-
if (null == blob)
266-
{
267-
throw new ResourceNotFoundException(String.Format(Resources.BlobNotFound, blobName, containerName));
268-
}
269-
else
242+
AzureStorageBlob outputBlob = new AzureStorageBlob(blobClient, localChannel.StorageContext, blobProperties, ClientOptions);
243+
if (this.IncludeTag.IsPresent)
244+
{
245+
IDictionary<string, string> tags = (await blobClient.GetTagsAsync(null, this.CmdletCancellationToken).ConfigureAwait(false)).Value.Tags;
246+
if (tags != null)
270247
{
271-
OutputStream.WriteObject(taskId, new AzureStorageBlob(blob, localChannel.StorageContext, ClientOptions));
248+
outputBlob.Tags = tags.ToHashtable();
249+
outputBlob.TagCount = tags.Count;
272250
}
273251
}
252+
OutputStream.WriteObject(taskId, outputBlob);
274253
}
275254
}
276255

@@ -297,93 +276,48 @@ internal async Task ListBlobsByPrefix(long taskId, IStorageBlobManagement localC
297276
BlobContinuationToken continuationToken = ContinuationToken;
298277
string track2ContinuationToken = this.ContinuationToken is null ? null : this.ContinuationToken.NextMarker;
299278

300-
if (UseTrack2Sdk()) // For new feature only available on Track2 SDK, need list with Track2 SDK.
279+
BlobTraits blobTraits = BlobTraits.Metadata | BlobTraits.CopyStatus; // | BlobTraits.Tags;
280+
BlobStates blobStates = BlobStates.Snapshots;
281+
if (includeDeleted)
301282
{
302-
BlobTraits blobTraits = BlobTraits.Metadata | BlobTraits.CopyStatus; // | BlobTraits.Tags;
303-
BlobStates blobStates = BlobStates.Snapshots;
304-
if (includeDeleted)
305-
{
306-
blobStates = blobStates | BlobStates.Deleted;
307-
}
308-
if (includeVersion)
309-
{
310-
blobStates = blobStates | BlobStates.Version;
311-
}
312-
if (IncludeTag.IsPresent)
313-
{
314-
blobTraits = blobTraits | BlobTraits.Tags;
315-
}
316-
317-
do
318-
{
319-
requestCount = Math.Min(listCount, MaxListCount);
320-
realListCount = 0;
321-
IEnumerator<Page<BlobItem>> enumerator = track2container.GetBlobs(blobTraits, blobStates, prefix, CmdletCancellationToken)
322-
.AsPages(track2ContinuationToken, requestCount)
323-
.GetEnumerator();
324-
325-
Page<BlobItem> page;
326-
enumerator.MoveNext();
327-
page = enumerator.Current;
328-
foreach (BlobItem item in page.Values)
329-
{
330-
if (blobFilter == null || blobFilter(item.Name))
331-
{
332-
ClientOptions.TrimBlobNameSlashes = false;
333-
OutputStream.WriteObject(taskId, GetAzureStorageBlob(item, track2container, localChannel.StorageContext, page.ContinuationToken, ClientOptions));
334-
}
335-
realListCount++;
336-
}
337-
track2ContinuationToken = page.ContinuationToken;
338-
339-
if (InternalMaxCount != int.MaxValue)
340-
{
341-
listCount -= realListCount;
342-
}
343-
} while (listCount > 0 && !string.IsNullOrEmpty(track2ContinuationToken));
283+
blobStates = blobStates | BlobStates.Deleted;
344284
}
345-
else
285+
if (includeVersion)
346286
{
347-
BlobRequestOptions requestOptions = RequestOptions;
348-
bool useFlatBlobListing = true;
349-
BlobListingDetails details = BlobListingDetails.Snapshots | BlobListingDetails.Metadata | BlobListingDetails.Copy;
350-
if (includeDeleted)
351-
{
352-
details = details | BlobListingDetails.Deleted;
353-
}
287+
blobStates = blobStates | BlobStates.Version;
288+
}
289+
if (IncludeTag.IsPresent)
290+
{
291+
blobTraits = blobTraits | BlobTraits.Tags;
292+
}
354293

355-
do
294+
do
295+
{
296+
requestCount = Math.Min(listCount, MaxListCount);
297+
realListCount = 0;
298+
IEnumerator<Page<BlobItem>> enumerator = track2container.GetBlobs(blobTraits, blobStates, prefix, CmdletCancellationToken)
299+
.AsPages(track2ContinuationToken, requestCount)
300+
.GetEnumerator();
301+
302+
Page<BlobItem> page;
303+
enumerator.MoveNext();
304+
page = enumerator.Current;
305+
foreach (BlobItem item in page.Values)
356306
{
357-
requestCount = Math.Min(listCount, MaxListCount);
358-
realListCount = 0;
359-
BlobResultSegment blobResult = await localChannel.ListBlobsSegmentedAsync(container, prefix, useFlatBlobListing,
360-
details, requestCount, continuationToken, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false);
361-
362-
foreach (IListBlobItem blobItem in blobResult.Results)
363-
{
364-
CloudBlob blob = blobItem as CloudBlob;
365-
366-
if (blob == null)
367-
{
368-
continue;
369-
}
370-
371-
if (blobFilter == null || blobFilter(blob.Name))
372-
{
373-
WriteCloudBlobObject(taskId, localChannel, blob, blobResult.ContinuationToken);
374-
realListCount++;
375-
}
376-
}
377-
378-
if (InternalMaxCount != int.MaxValue)
307+
if (blobFilter == null || blobFilter(item.Name))
379308
{
380-
listCount -= realListCount;
309+
ClientOptions.TrimBlobNameSlashes = false;
310+
OutputStream.WriteObject(taskId, GetAzureStorageBlob(item, track2container, localChannel.StorageContext, page.ContinuationToken, ClientOptions));
381311
}
312+
realListCount++;
313+
}
314+
track2ContinuationToken = page.ContinuationToken;
382315

383-
continuationToken = blobResult.ContinuationToken;
316+
if (InternalMaxCount != int.MaxValue)
317+
{
318+
listCount -= realListCount;
384319
}
385-
while (listCount > 0 && continuationToken != null);
386-
}
320+
} while (listCount > 0 && !string.IsNullOrEmpty(track2ContinuationToken));
387321
}
388322

389323
public static AzureStorageBlob GetAzureStorageBlob(BlobItem blobItem, BlobContainerClient track2container, AzureStorageContext context, string continuationToken = null, BlobClientOptions options = null)

0 commit comments

Comments
 (0)