Use blob index tags to manage and find data with .NET
This article shows how to use blob index tags to manage and find data using the Azure Storage client library for .NET.
Prerequisites
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for .NET. To learn about setting up your project, including package installation, adding
using
directives, and creating an authorized client object, see Get started with Azure Blob Storage and .NET. - The authorization mechanism must have permissions to work with blob index tags. To learn more, see the authorization guidance for the following REST API operations:
About blob index tags
Blob index tags categorize data in your storage account using key-value tag attributes. These tags are automatically indexed and exposed as a searchable multi-dimensional index to easily find data. This article shows you how to set, get, and find data using blob index tags.
To learn more about this feature along with known issues and limitations, see Manage and find Azure Blob data with blob index tags.
Set tags
You can set index tags if your code has authorized access to blob data through one of the following mechanisms:
- Security principal that is assigned an Azure RBAC role with the Microsoft.Storage/storageAccounts/blobServices/containers/blobs/tags/write action. The Storage Blob Data Owner is a built-in role that includes this action.
- Shared Access Signature (SAS) with permission to access the blob's tags (
t
permission) - Account key
For more information, see Setting blob index tags.
You can set tags by using either of the following methods:
The following example performs this task.
public static async Task SetTags(BlobClient blobClient)
{
Dictionary<string, string> tags =
new Dictionary<string, string>
{
{ "Sealed", "false" },
{ "Content", "image" },
{ "Date", "2020-04-20" }
};
await blobClient.SetTagsAsync(tags);
}
You can delete all tags by passing an empty [Dictionary] into the SetTags or SetTagsAsync method as shown in the following example.
Dictionary<string, string> noTags = new Dictionary<string, string>();
await blobClient.SetTagsAsync(noTags);
Related articles |
---|
Manage and find Azure Blob data with blob index tags |
Set Blob Tags (REST API) |
Get tags
You can get index tags if your code has authorized access to blob data through one of the following mechanisms:
- Security principal that is assigned an Azure RBAC role with the Microsoft.Storage/storageAccounts/blobServices/containers/blobs/tags/read action. The Storage Blob Data Owner is a built-in role that includes this action.
- Shared Access Signature (SAS) with permission to access the blob's tags (
t
permission) - Account key
For more information, see Getting and listing blob index tags.
You can get tags by using either of the following methods:
The following example performs this task.
public static async Task GetTags(BlobClient blobClient)
{
Response<GetBlobTagResult> tagsResponse = await blobClient.GetTagsAsync();
foreach (KeyValuePair<string, string> tag in tagsResponse.Value.Tags)
{
Console.WriteLine($"{tag.Key}={tag.Value}");
}
}
Filter and find data with blob index tags
You can use index tags to find and filter data if your code has authorized access to blob data through one of the following mechanisms:
- Security principal that is assigned an Azure RBAC role with the Microsoft.Storage/storageAccounts/blobServices/containers/blobs/filter/action action. The Storage Blob Data Owner is a built-in role that includes this action.
- Shared Access Signature (SAS) with permission to filter blobs by tags (
f
permission) - Account key
For more information, see Finding data using blob index tags.
Note
You can't use index tags to retrieve previous versions. Tags for previous versions aren't passed to the blob index engine. For more information, see Conditions and known issues.
You can find data by using either of the following methods:
The following example finds all blobs tagged with a date that falls between a specific range.
public static async Task FindBlobsbyTags(BlobServiceClient serviceClient)
{
string query = @"""Date"" >= '2020-04-20' AND ""Date"" <= '2020-04-30'";
// Find Blobs given a tags query
Console.WriteLine("Find Blob by Tags query: " + query + Environment.NewLine);
List<TaggedBlobItem> blobs = new List<TaggedBlobItem>();
await foreach (TaggedBlobItem taggedBlobItem in serviceClient.FindBlobsByTagsAsync(query))
{
blobs.Add(taggedBlobItem);
}
foreach (var filteredBlob in blobs)
{
Console.WriteLine($"BlobIndex result: ContainerName= {filteredBlob.BlobContainerName}, " +
$"BlobName= {filteredBlob.BlobName}");
}
}
Resources
To learn more about how to use index tags to manage and find data using the Azure Blob Storage client library for .NET, see the following resources.
REST API operations
The Azure SDK for .NET contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar .NET paradigms. The client library methods for managing and using blob index tags use the following REST API operations:
- Get Blob Tags (REST API)
- Set Blob Tags (REST API)
- Find Blobs by Tags (REST API)
Client library resources
See also
Feedback
Submit and view feedback for