我正在尝试将图像添加到 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
属性的值,因为它包含无效字符 (/
)。
有关 PartitionKey
和 RowKey
中不允许使用的字符的列表,请参阅此页面: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/