Alexander Farber
Alexander Farber

Reputation: 22978

Despite calling SetMetadataAsync() and then UploadBlobAsync() no metadata or tags is found in the blob

In my SF application I call the following C# code:

private readonly BlobContainerClient containerClient = null;
private readonly IDictionary<string, string> metadata = new Dictionary<string, string>();

DateTime now = DateTime.Now;
this.metadata.Clear();
this.metadata.Add("tag1", blobBasicName);
blobName = string.Format("{0}/{1:00}_{2:00}_{3:00}/{4:00}_{5:00}_{6:00}",
    blobBasicName, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);

using (MemoryStream ms = new MemoryStream(objectToWriteToBlob))
{
    await this.containerClient.SetMetadataAsync(metadata);
    await this.containerClient.UploadBlobAsync(blobName, ms);
}

And can see the uploaded blobs in Azure portal, but the metadata is not there:

azure screenshot

Also I try to retrieve the list of blobs for the storage account, and the blob.tags is not set there either:

container_client = ContainerClient.from_connection_string(conn_str, container_name=container_name)

blob_list = []
print('Fetching blobs from %s...' % container_name)
for blob in container_client.list_blobs():
    if blob.name.startswith(MY_BLOB_PREFIX) and blob.size > 500:
        print('blob name = %s, tags = %s' % (blob.name, blob.tags))
        blob_list.append(blob)
print('Fetched %d non-empty blobs from %s' % (len(blob_list), container_name))

cmd screenshot

Upvotes: 0

Views: 1027

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

You're actually setting the metadata for the blob container instead of blob itself, since you're using the SetMetadataAsync method based on blob container object.

If you want to set metadata for a blob during uploading, you should use the code below:

private readonly BlobContainerClient containerClient = null;
private readonly IDictionary<string, string> metadata = new Dictionary<string, string>();

DateTime now = DateTime.Now;
this.metadata.Clear();
this.metadata.Add("tag1", blobBasicName);
blobName = string.Format("{0}/{1:00}_{2:00}_{3:00}/{4:00}_{5:00}_{6:00}",
    blobBasicName, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);

var blobclient = containerClient.GetBlobClient(blobName);


using (MemoryStream ms = new MemoryStream(objectToWriteToBlob))
{
     blobclient.Upload(ms, null, metadata, null, null, null);
}

Upvotes: 3

Related Questions