簡體   English   中英

想要使用新的 sdk - Azure.Storage.Blobs package 從 blob 內的文件夾下載和上傳文件

[英]Want to download and upload file from a folder inside the blob using new sdk - Azure.Storage.Blobs package

I want to download and upload a json file in a folder by using path and jsonContent in string for in new sdk - Azure.Storage.Blobs package. 我可以按照下面的代碼使用舊庫來做到這一點 -

上傳代碼 -

`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();
    }

包含 json 文件的文件夾的快照

試試下面的代碼。 基本上我們正在創建一個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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM