c# - 尝试将 Azure Blob 存储 URI 添加到 Azure 存储表时出错

标签 c# azure azure-blob-storage azure-table-storage azure-storage-emulator

我正在尝试将图像添加到 Azure Blob 存储(程序的这部分工作正常)。

将每个图像放入 Blob 存储后,我想创建一个 Azure 存储表,其中的实体具有两列。图像类别和图像 URI。

当我尝试插入 Azure 存储表时,应用程序进入“中断模式”。

最终,这将由移动应用程序使用,该应用程序使用表格选择图像 URI 作为 GridView 的图像源,因此我需要完整的 URI。

我已经对类别进行了硬编码,以尝试缩小问题范围。

我在开发过程中使用 Azure 存储模拟器。仅供引用。

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AzureEmulatorTests
{
    class Program
    {
        static async Task Main(string[] args)
        {
            const string localPath = "./data/";
            const string containerName = "mycontainer";
            const string tableName = "mytable";

            var storageAccount = CloudStorageAccount.Parse(@"UseDevelopmentStorage=true");

            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync();

            var tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference(tableName);
            await table.CreateIfNotExistsAsync();

            string[] filenames = Directory.GetFiles(localPath);

            foreach (var images in filenames)
            {
                string _imageBlobReference = Guid.NewGuid() + ".jpg";

                var blob = container.GetBlockBlobReference(_imageBlobReference);
                await blob.UploadFromFileAsync(images);
                string blobUrl = blob.Uri.AbsoluteUri;

                ImageFile image = new ImageFile("Birthday", blobUrl);

                await table.ExecuteAsync(TableOperation.Insert(image));
            }

            Console.WriteLine("Files Uploaded... Press any key to continue.");
            Console.ReadKey();
        }
    }

    class ImageFile : TableEntity
    {
        public ImageFile(string Category, string ImageURL)
        {
            PartitionKey = Category;
            RowKey = ImageURL;
        }
    }
}

最佳答案

基本上,问题在于您的 RowKey 属性的值,因为它包含无效字符 (/)。

有关 PartitionKeyRowKey 中不允许使用的字符的列表,请参阅此页面:https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-the-table-service-data-model .

要解决此问题,请对整个 URL 进行 URL 编码,然后尝试保存。这应该可行。

关于c# - 尝试将 Azure Blob 存储 URI 添加到 Azure 存储表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61628832/

相关文章:

c# - 如何将文件上传到 blob 存储并从附件中的 cosmosdb 文档引用它

python-3.x - 使用 Python 将 Azure Blob 存储数据直接推送到 POST 请求中

c# - MySQL 连接不会关闭

c# - 从 BitmapSource 复制到 WriteableBitmap

c# - Crystal Report 询问简单数据集的登录信息。如何隐藏它?

node.js - 当我传递多个范围时,Microsoft Graph 权限范围引发错误

Azure IoT Edge EdgeHub 模块无法启动

azure - Azure Blob 存储是否已备份

image - Azure,存储和部署静态内容(例如图像/css)的最佳方式?

c# - 我的自定义 BinaryHeap 的顺序并不总是正确的