我有这样一段代码,它获取一个BLOB存储文件并提取它,当我在我的计算机上测试保存结果时,它可以工作.

现在,我希望将提取的结果上传到另一个BLOB存储,而不是将文件保存在本地计算机上.我的挑战取决于如何将结果提取为字节或流,以便我可以上传,我确实try 了这一点,我没有收到错误,但BLOB是0字节.如有任何建议,我们不胜感激.

var blobEndPointSrc = "https://storageaccountname.blob.core.windows.net/data-source-raw?etc...";
var blobContainerClientSrc = GetBlobServiceClientSAS(blobEndPointSrc);
var blobClientSrc = blobContainerClientSrc.GetBlobClient("file.tar.gz");
var blobDownloadInfo = blobClientSrc.OpenRead();
using Stream gzipStream = new GZipInputStream(blobDownloadInfo);

// test on local drive and works.
using var tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
tarArchive.ExtractContents(@"D:\file");

现在正在try 获取结果并将其上传到BLOB存储:

// using var tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
// tarArchive.ExtractContents(@"D:\file");

var tarBuffer = TarBuffer.CreateInputTarBuffer(gzipStream);
Stream result = new MemoryStream();
TarBuffer.CreateOutputTarBuffer(result);

var blobEndPointDst = = "https://storageaccountname2.blob.core.windows.net/data-source-raw?etc...";
var blobContainerClientDst = GetBlobServiceClientSAS(blobEndPointDst);
var blobClientDst = blobContainerClient1.GetBlobClient("extracted_file");
blobClientDst.Upload(result, true);

所有的BLOB代码都可以很好地用于上传和下载.但提取结果的转换最终在上传的文件中给出了0个字节.

如果建议另一个解压缩库或其他解决方案,也是可以的.

推荐答案

I tried the following code to extract the tar.gz file from Blob Storage and upload the extracted result to another Blob Storage.

Code :

using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using Azure.Storage.Blobs;

namespace ExtractAndUpload
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceBlobConnectionString = "<sourceblobconne_string>";
            string destinationBlobConnectionString = "<destinationblobconne_string>";

            string sourceContainerName = "<sourceContainer_name>";
            string sourceBlobName = "<blobname.tar.gz>";
            string destinationContainerName = "<destinationContainer_name>";
            string destinationBlobName = "<destinationBlob_name>";

            var sourceBlobServiceClient = new BlobServiceClient(sourceBlobConnectionString);
            var sourceBlobContainerClient = sourceBlobServiceClient.GetBlobContainerClient(sourceContainerName);
            var sourceBlobClient = sourceBlobContainerClient.GetBlobClient(sourceBlobName);

            var downloadResponse = sourceBlobClient.Download();
            using (Stream sourceStream = downloadResponse.Value.Content)
            {
                using (var gzipStream = new GZipInputStream(sourceStream))
                {
                    using (var tarInputStream = new TarInputStream(gzipStream))
                    {
                        TarEntry entry;
                        while ((entry = tarInputStream.GetNextEntry()) != null)
                        {
                            if (entry.IsDirectory)
                                continue;
                            using (var result = new MemoryStream())
                            {
                                tarInputStream.CopyEntryContents(result);
                                result.Position = 0;
                                var destinationBlobServiceClient = new BlobServiceClient(destinationBlobConnectionString);
                                var destinationBlobContainerClient = destinationBlobServiceClient.GetBlobContainerClient(destinationContainerName);
                                var destinationBlobClient = destinationBlobContainerClient.GetBlobClient(destinationBlobName);
                                destinationBlobClient.Upload(result, true);
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Extraction and upload completed successfully.");
        }
    }
}

Output :

运行成功,如下图:

Extraction and upload completed successfully.

enter image description here

Sourceblob :

enter image description here

Destinationblob :

提取的文件数据已成功保存到目标BLOB,如下所示.

enter image description here

Csharp相关问答推荐

C#中的两个线程之间读写浮点类型

使用GeneratedComInterfaceProperty的.NET 8 COM类对于VB 6/SYS或ALEViewer不可见

等待限制选项似乎不适用于频道

在依赖性注入和继承之间进行 Select

EF Core判断是否应用了AsSplitQuery()

如何从泛型方法返回一个可为空的T,其中T:notnull?

System. InvalidOperationException:无法将数据库中的字符串值i转换为映射的ItemType枚举中的任何值''''

什么是通过反射创建类的泛型接口方法的正确方法?

属性getter和setter之间的空性不匹配?

Blazor在FluentButton onClick事件上设置参数

CS1660无法将lambda表达式转换为类型INavigationBase,因为它不是委托类型

集合表达式没有目标类型

什么时候接受(等待)信号灯?尽可能的本地化?

如何将DotNet Watch与发布配置和传递给应用程序的参数一起使用?

C#多键字典和TryGetValue

Blazor服务器项目中的Blazor/.NET 8/Web API不工作

使用未赋值的、传递的局部变量

FakeItEasy自动嘲弄内容

为什么当我try 为玩家角色设置动画时,没有从文件夹中拉出正确的图像?

带有类约束的C#泛型