您可以在上传Azure CloudBlockBlob的同时设置元数据吗? [英] Can you set metadata on an Azure CloudBlockBlob at the same time as uploading it?

查看:138
本文介绍了您可以在上传Azure CloudBlockBlob的同时设置元数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,当前我正在使用CloudBlockBlob.UploadFromStreamAsync上传一个CloudBlockBlob,然后立即在其上设置一堆用户元数据.

I have a situation in which I currently upload a CloudBlockBlob using CloudBlockBlob.UploadFromStreamAsync, and then immediately afterward I set a bunch of user metadata on it.

问题是:我有一个事件网格事件,该事件在上载Blob时触发,但是事件处理程序需要元数据.长话短说,这是一个竞赛条件,在这种情况下,我必须希望"在事件处理程序响应块Blob上传之前已经设置了元数据.

The issue is: I have an Event Grid event which is triggered when the blob is uploaded, but the event handler requires the metadata. Long story short, it's a race condition wherein I have to "hope" that the metadata has been set before my event handler responds to the block blob upload.

是否有某种方式可以在单个操作中上传块Blob(文件)并设置其元数据?

Is there some way to both upload the block blob (file) and set its metadata in a single operation?

推荐答案

正如juunas所说,您可以在调用Upload之前在Blob上设置元数据. 我使用.Net Console进行了一些演示,您可以参考一下.

As juunas said, you could just set the metadata on the blob before calling Upload. I make a little demo with .Net Console you could refer to.

public static void Main(string[] args)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.GetContainerReference("container");
    CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("hello.txt");
    MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes("aaaaaa"));
    msWrite.Position = 0;
    cloudBlockBlob.Metadata["category"] = "guidance";
    using (msWrite)
    {
        cloudBlockBlob.UploadFromStream(msWrite);
    }
 }

门户网站上的blob中的输出:

The output in blob on portal:

这篇关于您可以在上传Azure CloudBlockBlob的同时设置元数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆