【问题标题】:Creating and Uploading Append Blobs to store in Azure Container创建和上传附加 Blob 以存储在 Azure 容器中
【发布时间】:2021-06-11 16:42:08
【问题描述】:

每次从服务总线传入消息时,我都会尝试将新的附加 blob 文件上传到容器。我不想附加到已经存在的 blob 上。我想创建一个全新的附加 blob 并将其添加到末尾。

这可能吗?

我正在看这篇文章,但是当他们进入内容部分时无法安静地理解他们的意思:https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-storage-blob/12.1.1/classes/appendblobclient.html#appendblock

这是我目前的代码:

public static async void StoreToBlob(Services service)
    {
        //Serealize Object 
        var sender = JsonConvert.SerializeObject(service);

        // Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

        // Create the container and return a container client object
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

        // Create the container if it doesn't already exist.  
        await containerClient.CreateIfNotExistsAsync();

        //Reference to blob
        AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient("services" + Guid.NewGuid().ToString() + ".json");

        // Create the blob. 
        appendBlobClient.Create();

        await appendBlobClient.AppendBlock(sender, sender.Length); //here is where I am having an issue
    }

【问题讨论】:

  • 您面临哪些问题?
  • 抱歉,我正在尝试学习如何创建一个 json 文件,向其中添加一个序列化对象,然后将其添加到我的容器中。当我到达 AppendBlock 部分时,它要求我提供流,但是,我没有文件。
  • 我需要先创建一个然后添加它吗? @Gaurav Mantri
  • @GauravMantri ^

标签: c# azure append azure-container-service


【解决方案1】:

您可以尝试以下类似的方法(未经测试的代码):

byte[] blockContent = Encoding.UTF8.GetBytes(sender);
using (var ms = new MemoryStream(blockContent))
{
    appendBlobClient.AppendBlock(ms, blockContent.Length);
}

本质上,我们将字符串转换为字节数组,从中创建一个流,然后上传该流。

【讨论】:

  • 谢谢。我插入了该代码,并且不得不取出 Length 部分 b/c 该方法需要字节而不是长度。然后摆脱了“等待”,因为系统说我不需要它。
  • 运行后。我的代码停在这里:“appendBlobClient.Create();”并说该文件不存在。我试图弄清楚为什么他们说它不存在,如果我正在尝试创建的那个。
  • 没关系,我让它工作了!谢谢你的帮助:)
  • 太棒了。我很高兴听到这个消息。
猜你喜欢
  • 2011-02-24
  • 2013-02-23
  • 2018-11-21
  • 2021-07-24
  • 1970-01-01
  • 2016-10-16
  • 2020-03-28
  • 2020-11-12
  • 2023-03-23
相关资源
最近更新 更多