public async Task FindBlobsByTagAsync_Error() { // Arrange BlobServiceClient service = InstrumentClient( new BlobServiceClient( GetServiceClient_SharedKey().Uri, GetOptions())); // Act await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>( service.FindBlobsByTagsAsync("\"key\" = 'value'").AsPages().FirstAsync(), e => Assert.AreEqual(BlobErrorCode.NoAuthenticationInformation.ToString(), e.ErrorCode)); }
public async Task FindBlobsByTagAsync() { // Arrange BlobServiceClient service = GetServiceClient_SharedKey(); await using DisposingContainer test = await GetTestContainerAsync(); string blobName = GetNewBlobName(); AppendBlobClient appendBlob = InstrumentClient(test.Container.GetAppendBlobClient(blobName)); string tagKey = "myTagKey"; string tagValue = "myTagValue"; Dictionary <string, string> tags = new Dictionary <string, string> { { tagKey, tagValue } }; AppendBlobCreateOptions options = new AppendBlobCreateOptions { Tags = tags }; await appendBlob.CreateAsync(options); string expression = $"\"{tagKey}\"='{tagValue}'"; // It takes a few seconds for Filter Blobs to pick up new changes await Delay(2000); // Act List <TaggedBlobItem> blobs = new List <TaggedBlobItem>(); await foreach (Page <TaggedBlobItem> page in service.FindBlobsByTagsAsync(expression).AsPages()) { blobs.AddRange(page.Values); } // Assert TaggedBlobItem filterBlob = blobs.Where(r => r.BlobName == blobName).FirstOrDefault(); if (_serviceVersion >= BlobClientOptions.ServiceVersion.V2020_04_08) { Assert.AreEqual(1, filterBlob.Tags.Count); Assert.AreEqual("myTagValue", filterBlob.Tags["myTagKey"]); } else { Assert.IsNotNull(filterBlob); } }
public async Task FindBlobsByTagAsync_AccountSas(AccountSasPermissions accountSasPermissions) { // Arrange BlobServiceClient service = GetServiceClient_SharedKey(); await using DisposingContainer test = await GetTestContainerAsync(); string blobName = GetNewBlobName(); AppendBlobClient appendBlob = InstrumentClient(test.Container.GetAppendBlobClient(blobName)); string tagKey = "myTagKey"; string tagValue = "myTagValue"; Dictionary <string, string> tags = new Dictionary <string, string> { { tagKey, tagValue } }; AppendBlobCreateOptions options = new AppendBlobCreateOptions { Tags = tags }; await appendBlob.CreateAsync(options); string expression = $"\"{tagKey}\"='{tagValue}'"; // It takes a few seconds for Filter Blobs to pick up new changes await Delay(2000); // Act SasQueryParameters sasQueryParameters = GetNewAccountSas(permissions: accountSasPermissions); BlobServiceClient sasServiceClient = new BlobServiceClient(new Uri($"{service.Uri}?{sasQueryParameters}"), GetOptions()); List <TaggedBlobItem> blobs = new List <TaggedBlobItem>(); await foreach (Page <TaggedBlobItem> page in sasServiceClient.FindBlobsByTagsAsync(expression).AsPages()) { blobs.AddRange(page.Values); } // Assert TaggedBlobItem filterBlob = blobs.Where(r => r.BlobName == blobName).FirstOrDefault(); Assert.IsNotNull(filterBlob); }
/// <summary> /// list blobs by blob Tag /// </summary> /// <param name="containerName">container name</param> /// <param name="prefix">blob preifx</param> /// <returns>An enumerable collection of IListBlobItem</returns> internal async Task ListBlobsByTag(long taskId, IStorageBlobManagement localChannel, string tagFilterSqlExpression) { BlobServiceClient blobServiceClient = Util.GetTrack2BlobServiceClient(localChannel.StorageContext, ClientOptions); int listCount = InternalMaxCount; int MaxListCount = 5000; int requestCount = MaxListCount; int realListCount = 0; BlobContinuationToken continuationToken = ContinuationToken; string track2ContinuationToken = this.ContinuationToken is null ? null : this.ContinuationToken.NextMarker; do { requestCount = Math.Min(listCount, MaxListCount); realListCount = 0; IAsyncEnumerator <Page <BlobTagItem> > enumerator = blobServiceClient.FindBlobsByTagsAsync(tagFilterSqlExpression, CmdletCancellationToken) .AsPages(track2ContinuationToken, requestCount) .GetAsyncEnumerator(); Page <BlobTagItem> page; await enumerator.MoveNextAsync().ConfigureAwait(false); page = enumerator.Current; foreach (BlobTagItem item in page.Values) { BlobContainerClient track2container = blobServiceClient.GetBlobContainerClient(item.BlobContainerName); OutputStream.WriteObject(taskId, GetAzureStorageBlob(item, track2container, localChannel.StorageContext, page.ContinuationToken, ClientOptions)); realListCount++; } track2ContinuationToken = page.ContinuationToken; if (InternalMaxCount != int.MaxValue) { listCount -= realListCount; } } while (listCount > 0 && !string.IsNullOrEmpty(track2ContinuationToken)); }
public async static Task Execute(string connectionString, string containerName, List <ObjectInfoTableEntity> objInfos) { //https://azure.microsoft.com/en-us/blog/manage-and-find-data-with-blob-index-for-azure-storage-now-in-preview/ //https://docs.microsoft.com/en-us/azure/storage/blobs/storage-manage-find-blobs //https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-index-how-to containerName = containerName.ToLower(); var serviceClient = new BlobServiceClient(connectionString); var containerClient = serviceClient.GetBlobContainerClient(containerName); //create the container if needed Console.WriteLine($"Create Container - {containerName}"); await containerClient.CreateIfNotExistsAsync(); //upload all the blobs Console.WriteLine($"\n\nAdding:"); foreach (var objinfo in objInfos) { Console.WriteLine($" {objinfo}"); string file = $"{objinfo.PartitionKey}/{objinfo.RowKey}.txt"; var blobClient = containerClient.GetBlobClient(file); await blobClient.DeleteIfExistsAsync(); await blobClient.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes(RandomValueGen.GetRandomWords(50)))); await blobClient.SetTagsAsync(new Dictionary <string, string> { { "CustomerId", objinfo.CustomerId }, { "O365Id", objinfo.O365Id }, { "Created", objinfo.Created.ToString("s") }, { "Removed", objinfo.Removed.ToString("s") } }); } //pick a random object to play with var tofind = objInfos[RandomValueGen.GetRandomInt(0, objInfos.Count - 1)]; Console.WriteLine($"\n\nSearching for\n {tofind}"); var found = (await containerClient.GetBlobClient($"{tofind.PartitionKey}/{tofind.RowKey}.txt").GetTagsAsync()).Value; Console.WriteLine($"\n\nFound by Partition/Row key\n {found.ToJoinedString()}"); Console.WriteLine($"\n\nFind all revisions of a given object by hierarchy"); await foreach (var item in containerClient.GetBlobsByHierarchyAsync(delimiter: "/", prefix: tofind.PartitionKey + "/", traits: BlobTraits.Tags)) { if (item.IsBlob) { Console.WriteLine($" {item.Blob.Tags.ToJoinedString()} - {item.Blob.Name}"); } } Console.WriteLine($"\n\nFind all revisions of a given object by tags"); string query = $"@container = '{containerName}' " + $"AND \"CustomerId\" = '{tofind.CustomerId}' " + $"AND \"O365Id\" = '{tofind.O365Id}' "; await foreach (var item in serviceClient.FindBlobsByTagsAsync(query)) { var blobClient = containerClient.GetBlobClient(item.Name); var tags = (await blobClient.GetTagsAsync()).Value; Console.WriteLine($" {tags.ToJoinedString()} - {item.Name}"); } }