Is there a way to set metadata on a blob at the time of creation using current APIs without using Microsoft.Azure.Storage.Blob?

huangapple go评论60阅读模式
英文:

Is there a way to set metadata on a blob at the time of creation using current APIs without using Microsoft.Azure.Storage.Blob?

问题

重要提示:有一个类似的问题讨论了使用已弃用的 Microsoft.Azure.Storage.Blob。我正在寻找一个不依赖于已弃用包的解决方案。

我正在使用 Azure.Storage.Blobs 在 Azure 存储帐户中创建一个 Blob,并希望在创建时设置该 Blob 的元数据。

这在同一时刻完成很重要;Blob 的创建会触发一个 EventGrid 事件,进而调用一个函数来执行使用该 Blob 及其元数据的操作。如果在设置元数据之前触发了该函数,将会有问题。

是否有办法使用 Azure.Storage.Blobs 或类似的方式实现这一点?
(我不想使用 https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/)。

英文:

Important: There's a similar question which discusses using Microsoft.Azure.Storage.Blob which is deprecated. I am looking for a solution which does not rely on deprecated packages.

I am creating a blob in an Azure Storage Account using Azure.Storage.Blobs, and I would like to set metadata on that blob at the time of creation.

It's important that this is done at the same time; blob creation triggers an EventGrid event which in turn calls a function to perform an operation using that blob and its metadata. It would be problematic if the function is triggered before metadata is set.

Is there a way to do this using Azure.Storage.Blobs or similar?
(I do not want to use https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/).

答案1

得分: 1

如果你的content是类型为Stream,你可以使用UploadAsync(Stream, BlobUploadOptions, CancellationToken) 方法。你可以在BlobUploadOptionsMetadata属性中设置元数据。

你的代码可能会像这样:

var metadata = new Dictionary<string, string>
{
    {"key1", "value1"},
    {"key2", "value2"}
};

var blobUploadOptions = new BlobUploadOptions()
{
    Metadata = metadata
};

await blobClient.UploadAsync(content, blobUploadOptions);

这里是一个更详细的示例:https://github.com/Azure/azure-sdk-for-net/blob/47ea075bca473fe6e9928ff9893fbaa8a552f3a5/sdk/storage/Azure.Storage.Blobs/samples/Sample03_Migrations.cs#L630。

英文:

Assuming your content is of type Stream, you can use UploadAsync(Stream, BlobUploadOptions, CancellationToken) method. You can set the metadata in Metadata property in BlobUploadOptions.

Your code could be something like:

var metadata = new Dictionary<string, string>
{
    {"key1", "value1"},
    {"key2", "value2"}
};

var blobUploadOptions = new BlobUploadOptions()
{
    Metadata = metadata
};

await blobClient.UploadAsync(content, blobUploadOptions);

Here's a more detailed example of the same: https://github.com/Azure/azure-sdk-for-net/blob/47ea075bca473fe6e9928ff9893fbaa8a552f3a5/sdk/storage/Azure.Storage.Blobs/samples/Sample03_Migrations.cs#L630.

huangapple
  • 本文由 发表于 2023年3月23日 09:19:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定