Azure CloudBlob SetMetadata 失败,并显示“指定的元数据无效。它包含不允许的字符。”

发布于 2024-11-27 06:16:16 字数 637 浏览 0 评论 0原文

我很确定这是 Windows Azure SDK 的限制(使用最新的 1.4),但是必须有一种方法可以解决这个问题,而无需使用手动 REST...

代码:

CloudBlob blob = container.GetBlobReference(url); // works
blob.UploadByteArray(bytes); // works
blob.Metadata["test"] = "public, max-age=259200"; // works
// FAILS with "The metadata specified is invalid. It has characters that are not permitted."
blob.Metadata["x-ms-blob-cache-control"] = "public, max-age=259200";
blob.SetMetadata(); // FAILS due to the 2nd meta data

从我的测试中可以清楚地看出由于这些破折号“-”,客户端正在爆炸,但我想不出任何解决办法。设置缓存控制非常重要,也是常见的操作,这让我很困惑为什么我找不到其他人报告这个问题。

我还尝试过对数据进行编码,这在技术上是不必要的,但出于绝望,我还是这么做了。有想法吗?

I'm pretty sure this is a limitation of the Windows Azure SDK (Using the latest, 1.4), but there has got to be a way around this without using manual REST...

The code:

CloudBlob blob = container.GetBlobReference(url); // works
blob.UploadByteArray(bytes); // works
blob.Metadata["test"] = "public, max-age=259200"; // works
// FAILS with "The metadata specified is invalid. It has characters that are not permitted."
blob.Metadata["x-ms-blob-cache-control"] = "public, max-age=259200";
blob.SetMetadata(); // FAILS due to the 2nd meta data

It's pretty clear from my tests that the client is blowing up due to these dashes '-', but I cannot think of any way around this. Setting cache control is very important, and common operation, which baffles me as to why I cannot find anyone else reporting on this problem.

I've also tried encoding the data, which technically shouldn't be necessary, but out of desperation I did it anyway. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-12-04 06:16:16

毕竟,它最终成为一个愚蠢的 SDK 限制,有一个特定的属性反过来为您设置特定的元数据...我不介意将此属性作为帮助器,但我认为没有理由设置元数据直接不应该工作。

blob.Properties.CacheControl = "public, max-age=259200";
blob.UploadByteArray(bytes);

It ended up being a silly SDK limitation after all, there is a specific property which in turns sets that specific meta data for you... I don't mind having this property as a helper, but I see no reason why setting the meta directly shouldn't work.

blob.Properties.CacheControl = "public, max-age=259200";
blob.UploadByteArray(bytes);
风吹过旳痕迹 2024-12-04 06:16:16

今天您仍然会收到此消息,但替换了 nuget 包 Azure.Storage.Blobs 版本 12.14.1 中的“BlobContainerClient”。以下是既适用于 Azurite 又部署在 Azure 中的使用版本:

var blobClient = blobContainerClient.GetBlobClient(blobName);
    if (await blobClient.ExistsAsync())
    {
        // Azurite doesn't support tags and Azure doesn't support use of metadata so make it work case by case.
        var isLocalEnvironment = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
        if (!isLocalEnvironment)
        {
            // This solution works only in Azure.
            var tags = new Dictionary<string, string>
            {
                { "TagKey1", "value1" },
                { "TagKey2", "value2" },
            };

            await blobClient.SetTagsAsync(tags);
        }
        else
        {
            // This solution works in Azurite (it does work in Azure also - but only without special characters).
            BlobProperties properties = blobClient.GetProperties();

            properties.Metadata["TagKey1"] = "value1";
            properties.Metadata["TagKey2"] = "value2";

            await blobClient.SetMetadataAsync(properties.Metadata);
        }
    }

允许的特殊字符:

https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-tags?source=recommendations&tabs=azure-ad

You get this message still today but with the replacing 'BlobContainerClient' in nuget package Azure.Storage.Blobs, version 12.14.1. Below is a version of usage that works both with Azurite and deployed in Azure:

var blobClient = blobContainerClient.GetBlobClient(blobName);
    if (await blobClient.ExistsAsync())
    {
        // Azurite doesn't support tags and Azure doesn't support use of metadata so make it work case by case.
        var isLocalEnvironment = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
        if (!isLocalEnvironment)
        {
            // This solution works only in Azure.
            var tags = new Dictionary<string, string>
            {
                { "TagKey1", "value1" },
                { "TagKey2", "value2" },
            };

            await blobClient.SetTagsAsync(tags);
        }
        else
        {
            // This solution works in Azurite (it does work in Azure also - but only without special characters).
            BlobProperties properties = blobClient.GetProperties();

            properties.Metadata["TagKey1"] = "value1";
            properties.Metadata["TagKey2"] = "value2";

            await blobClient.SetMetadataAsync(properties.Metadata);
        }
    }

Allowed special characters:

https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-tags?source=recommendations&tabs=azure-ad

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文