简体   繁体   中英

How to add and retrive additional information for Azure Blob

Is there a way to add additional information for blob in Azure?

I want to store some relevant information, which connects the blob to other entity in in an a document database, for example a string which contains a JSON.

I know there is metadata for blob when I use Azure Storage explorer, but i want use it from code.

This a relevant question about this theme: Adding Description/Metadata to Azure Blob

And how can retrieve the blobs based on this metadata?

Have you checked this link ?

public static async Task AddContainerMetadataAsync(CloudBlobContainer container)
{
   // Add some metadata to the container.
   container.Metadata.Add("docType", "textDocuments");
   container.Metadata["category"] = "guidance";

   // Set the container's metadata.
   await container.SetMetadataAsync();
}

Keep in mind that

The name of your metadata must conform to the naming conventions for C# identifiers.

The first part of the question is answered by Mihail Stancescu, thank you!

The second part is not answered correctly yet. The Azure Search is a solution for it, but it is a totally other service. I want to solve this problem in my repository class. And i solved it.

Maybe it is interesting for someone else that is why I share my solution:

Behind the solution There is a metadata in AzureBlob, which has a string type. I serialized a object to String and store this string in metadata. When I need this information in any cases, I listing the with metadata in it. I reach this this functionality to passing the Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails.Metadata value to the blobListingDetails parameter in ListBlobs function.

When The blobs are arrived, I inmediatly Deserialized back from JSON to object. This mechasim is visible in LINQ Select:

.Select<Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob, T>(blob = > JsonConvert.DeserializeObject<T>(blob.Metadata["data"]))

After this, the LINQ type is T, so I can Apply the Expression on it in LINQ Where.

The complete solution is:

GetMany function

public IEnumerable<T> GetMany( Expression<Func<T, bool>> filter )
{
            return _AzureBlobCollection.BlobDirectory
                .ListBlobs( useFlatBlobListing: false, blobListingDetails: Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails.Metadata )
                .OfType<Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob>()
                .Select<Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob, T>( blob => JsonConvert.DeserializeObject<T>( blob.Metadata[ "data" ] ) )
                .Where( filter.Compile() );
}

This function can call like this: Repository repository = ..

IEnumerable files = repository.GetMany( f => f.Partner = "Microsoft" );

Base classes

where file class is:

public class ContractFile : File
{
    public string Partner { get; set; }
    public Date CreationDate { get; set; }
    public string Remarks { get; set; }
    public string Filename { get; set; }
}
...
public class File
{
    public String File { get; set; }
    public Stream Data { get; set; }
}

And the insert is following:

    public void AddOne( T file )
    {
        file.id = Guid.NewGuid().ToString();
        Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = _AzureBlobCollection.BlobDirectory.GetBlockBlobReference( file.id );

        blob.UploadFromStream( file.Data );
        blob.Metadata.Add( "data", JsonConvert.SerializeObject( file ) );
        blob.SetMetadata();
    }

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