【问题标题】:Upload string to Azure Blob将字符串上传到 Azure Blob
【发布时间】:2021-02-09 05:46:09
【问题描述】:

我查看了以下链接以将字符串上传到 azure blob。我的任务要求不允许我将字符串存储为文件。

Is there any way of writing file to Azure Blob Storage directly from C# application?

它在 WindowsAzure.Storage 中使用 CloudStorageAccount,已根据 this link 弃用

我正在尝试使用 Azure.Storage.Blobs 库。但是,不再有 this microsoft documentation 中的 UploadString 方法

有什么建议吗?谢谢

【问题讨论】:

    标签: c# .net-core azure-blob-storage


    【解决方案1】:

    您可以上传一个流,因此您可以将字符串的内容作为字节数组,创建一个内存流并上传。

    BlobContainerClient container = new BlobContainerClient(connectionString, "myContainer");
    container.Create();
    
    BlobClient blob = container.GetBlobClient("myString");
    
    var myStr = "Hello!"
    var content = Encoding.UTF8.GetBytes(myStr);
    using(var ms = new MemoryStream(content))
        blob.Upload(ms);
    

    【讨论】:

    • 当我下载和查看blob时,如何上传由新行分隔的相同字符串(比如我想将单词拆分为新行)?
    • @Deepak 在Encoding.UTF8.GetBytes之前的字符串中添加换行符。
    • 谢谢,@Gusman。在你说的那个之前,我是在Environment.NewLine 的帮助下做到的。
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2021-01-11
    • 2017-03-14
    • 2023-03-28
    • 2017-04-26
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多