简体   繁体   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. 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. I am able to do so using old library as per below code -我可以按照下面的代码使用旧库来做到这一点 -

UploadCode -上传代码 -

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

Download Code -下载代码 -

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 文件的文件夹的快照

Try the following code.试试下面的代码。 Basically we're creating an instance of BlockBlobClient and calling it's Upload and Download method for uploading and downloading.基本上我们正在创建一个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.

相关问题 无法使用 Azure.Storage.Blobs NuGet 包将文件上传到 Azure Blob 存储 - Unable to upload a file to Azure Blob Storage using Azure.Storage.Blobs NuGet package 如何使用新的 Azure.Storage.Blobs 命名空间仅获取 blob 中的文件夹名称 - How to get only folder name in blob using new Azure.Storage.Blobs namespace 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 使用新的 Azure.Storage.Blobs 时如何计算存储帐户中 Blob 存储容器的总大小 - How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs 如何使用 Azure.Storage.Blobs 上传 stream - How to upload stream with Azure.Storage.Blobs Azure.Storage.Blobs 文件夹结构 - Azure.Storage.Blobs Folder Structure 如何使用 Azure.Storage.Blobs BlobClient 检索 blob 目录路径中的 blob? - How to retrieve blobs within a blob directory path using the Azure.Storage.Blobs BlobClient? 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType? - Is there a way to get a blob's ContentType in Azure.Storage.Blobs? 用于 Azure Blob 管理的 Azure.Storage.Blobs 与 Microsoft.WindowsAzure.Storage.Blob dll - Azure.Storage.Blobs vs Microsoft.WindowsAzure.Storage.Blob dll for Azure Blob management
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM