简体   繁体   中英

Azure blob storage search using tags c#

I'm storing images in Azure blob storage and I need to be able to search the container for a specific file using the tags and not the name.

Here is my connection string:

<add key="blobConnString" value="DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=XXXXXXXXXXXX;EndpointSuffix=core.windows.net"/>

Here is my method:

private void FindFiles()
{
    var foundItems = new List<TaggedBlobItem>();
    var connectionString = ConfigurationManager.AppSettings["blobConnString"];
    var _client = new BlobServiceClient(connectionString);
    var blobs = _client.FindBlobsByTags("@container = 'images' AND 'invID' = '12345'");
    foreach (var blob in blobs)
    {
        foundItems.Add(blob);
    }
}

And here's the error I get:

Error: Azure.RequestFailedException: Value for one of the query parameters specified in the request URI is invalid.
RequestId:7434f0b1-501e-0001-07a5-4ef3e1000000
Time:2022-04-12T19:45:01.1081429Z
Status: 400 (Value for one of the query parameters specified in the request URI is invalid.)
ErrorCode: InvalidQueryParameterValue

Additional Information:
QueryParameterName: comp
QueryParameterValue: blobs
Reason:

Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidQueryParameterValue</Code><Message>Value for one of the query parameters specified in the request URI is invalid.
RequestId:7434f0b1-501e-0001-07a5-4ef3e1000000
Time:2022-04-12T19:45:01.1081429Z</Message><QueryParameterName>comp</QueryParameterName><QueryParameterValue>blobs</QueryParameterValue><Reason /></Error>

Headers:
Server: Microsoft-HTTPAPI/2.0
x-ms-request-id: 7434f0b1-501e-0001-07a5-4ef3e1000000
x-ms-client-request-id: c522b1bc-ce28-4c48-a593-dfbb6d81af66
x-ms-error-code: InvalidQueryParameterValue
Date: Tue, 12 Apr 2022 19:45:00 GMT
Content-Length: 376
Content-Type: application/xml

   at Azure.Storage.Blobs.ServiceRestClient.FilterBlobs(Nullable`1 timeout, String where, String marker, Nullable`1 maxresults, CancellationToken cancellationToken)
   at Azure.Storage.Blobs.BlobServiceClient.FindBlobsByTagsInternal(String marker, String expression, Nullable`1 pageSizeHint, Boolean async, CancellationToken cancellationToken)
   at Azure.Storage.Blobs.Models.FilterBlobsAsyncCollection.GetNextPageAsync(String continuationToken, Nullable`1 pageSizeHint, Boolean async, CancellationToken cancellationToken)
   at Azure.Core.Pipeline.TaskExtensions.EnsureCompleted[T](ValueTask`1 task)
   at Azure.Storage.StorageCollectionEnumerator`1.StoragePageable.GetEnumerator()+MoveNext()
   at CosmosGettingStartedTutorial.Program.FindFiles() in C:\Users\Drew\Downloads\DocumentDB-Quickstart-DotNet\sql-dotnet\CosmosGettingStartedTutorial\Program.cs:line 297
   at CosmosGettingStartedTutorial.Program.GetStartedDemoAsync() in C:\Users\Drew\Downloads\DocumentDB-Quickstart-DotNet\sql-dotnet\CosmosGettingStartedTutorial\Program.cs:line 78
   at CosmosGettingStartedTutorial.Program.Main(String[] args) in C:\Users\Drew\Downloads\DocumentDB-Quickstart-DotNet\sql-dotnet\CosmosGettingStartedTutorial\Program.cs:line 45
End of demo, press any key to exit.

UPDATE This method works to retrieve my blobs:

var connectionString = ConfigurationManager.AppSettings["blobConnString"];
        var blobContainer = new BlobContainerClient(connectionString, "images");
        var blobs = new List<string>();
        await foreach (var blob in blobContainer.GetBlobsAsync())
        {
            blobs.Add(blob.Name);
        }

        foreach (var blob in blobs)
        {
            Console.WriteLine(blob);
        }

However, this chunk of code doesn't work and continues to throw the above error verbatim:

var blobClent = new BlobServiceClient(connectionString);
        var foundItems = blobClent.FindBlobsByTags("invID = '12345'").ToList();
        foreach (var blob in foundItems)
        {
            Console.WriteLine(blob.BlobName + " from " + blob.BlobContainerName);
        }

The second part is what is throwing the error. Is there something wrong w/ the service account in Azure? Am I missing an Azure setting (even though I can read and right)?

After reproducing from our end, we observed this is due to the invalid tagFilterSqlExpression . Below is the code that worked for us.

Retrieving blobs from All containers

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=<Your_Account_Name>;AccountKey=<Your_Account_Key>;EndpointSuffix=core.windows.net";
BlobServiceClient blobServiceClient = new BlobServiceClient(ConnectionString);

var foundItems = new List<TaggedBlobItem>();

foreach (TaggedBlobItem blob in client.FindBlobsByTags("invID = '12345'"))
{
    foundItems.Add(blob);
}

foreach(var blob in foundItems)
{
    Console.WriteLine(blob.BlobName+" from "+blob.BlobContainerName);
}

RESULT:

在此处输入图像描述

Retrieving blobs from a particular containers

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=<Your_Account_Name>;AccountKey=<Your_Account_Key>;EndpointSuffix=core.windows.net";
BlobServiceClient blobServiceClient = new BlobServiceClient(ConnectionString);

var foundItems = new List<TaggedBlobItem>();

foreach (TaggedBlobItem blob in client.FindBlobsByTags("@container = 'container1' AND invID = '12345'"))
{
    foundItems.Add(blob);
}

foreach(var blob in foundItems)
{
    Console.WriteLine(blob.BlobName);
}

RESULT:

在此处输入图像描述

The problem was a setting on the storage account in Azure. Hierarchical namespacing was causing the entire problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM