英文:
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)
方法。你可以在BlobUploadOptions
的Metadata
属性中设置元数据。
你的代码可能会像这样:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论