Reputation: 1608
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?
Upvotes: 0
Views: 464
Reputation: 1608
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();
}
Upvotes: 0
Reputation: 4138
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.
Upvotes: 5