gpt4 book ai didi

c# - 想要使用新的 sdk - Azure.Storage.Blobs 包从 blob 内的文件夹下载和上传文件

转载 作者:行者123 更新时间:2023-12-03 02:41:12 24 4
gpt4 key购买 nike

我想使用新 sdk - Azure.Storage.Blobs 包中的字符串中的路径和 jsonContent 下载并上传文件夹中的 json 文件。我可以按照下面的代码使用旧库来做到这一点 -

上传代码-

`public async Task<bool> UploadToBlob(string path, string jsonContentString)
{
CloudBlobContainer container = _cloudBlobClient.GetContainerReference(Constant.ContainerName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference("dose/NotificationDefinition/dose_dosedailyreport.json");
await blockBlob.UploadTextAsync(jsonContentString);
return true;
}`

下载代码 -

public async Task<string> GetDataFromStorage()
{
CloudBlobContainer container = _cloudBlobClient.GetContainerReference(Constant.ContainerName);
var blockBlob = container.GetBlockBlobReference("dose/NotificationDefinition/dose_dosedailyreport.json");
return await blockBlob.DownloadTextAsync();
}

Snapshot of Folder containing json file

最佳答案

尝试以下代码。基本上我们正在创建 BlockBlobClient 的实例并将其称为 UploadDownload上传和下载方法。

    static void UploadDownloadTest()
{
var blobName = "dose/NotificationDefinition/dose_dosedailyreport.json";
var containerName = "test";
var connectionString = "UseDevelopmentStorage=true";
var blockBlobClient = new BlockBlobClient(connectionString, containerName, blobName);
var jsonContentString = "This is the data I wish to upload";
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonContentString)))
{
var httpHeaders = new BlobHttpHeaders()
{
ContentType = "application/json"
};
blockBlobClient.Upload(ms, httpHeaders);
}
Console.WriteLine("Upload Successful!");
var downloadResponse = blockBlobClient.Download().Value;
using (var stream = downloadResponse.Content)
{
byte[] buffer = new byte[downloadResponse.ContentLength];
stream.Read(buffer, 0, buffer.Length);
var responseData = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Blob contents....");
Console.WriteLine(responseData);
}
}

关于c# - 想要使用新的 sdk - Azure.Storage.Blobs 包从 blob 内的文件夹下载和上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61205188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com