我的C#项目用于执行两个操作:

  1. 下载SharePoint文件并
  2. 通过将下载的流保存到Azure BLOB中来存储该文件.

我最近得到了一个要求,要求在第一步和第二步之间再增加一个步骤. 它是将此流的副本保存为本地文件夹中的文件,然后将其保存到Azure Blob. 当我使用stream Object.CopyTo(LocalFileLocation)将该流复制到本地时,源流将变为空白,BLOB文件的大小变为0.

string content = string.Empty;
var stream = await this.DownloadFromSPO(pipeline).ConfigureAwait(false);
if (stream == null)
{                
    return false;
}
    
// saving stream as file into local folder
                
string filePath2 = Path.Combine("C:\\localLocation", pipeline.Name);
                
using (StreamReader reader = new StreamReader(stream))
{      
    content = reader.ReadToEnd();         
    FileStream outputFileStream = new FileStream(filePath2, FileMode.Create);
    stream.Position = 0;                
    stream.CopyTo(outputFileStream);
}          
    
                
stream = helper.GetStream(content);
// Upload the file to BLOB Storage using drive item id as the blob file name
CloudBlob blob = null;
await AzureRetryHelper.OperationWithBasicRetryAsync(async () =>
{
    blob = await this.blobHelper.UploadFileToBlob(
        stream,
        this.config.GetConfigValue(Constants.DataContainerName),
        pipeline.BlobNativeFilePath,
        true,
        pipeline.MimeType).ConfigureAwait(false);
}).ConfigureAwait(false);

这就是我try 添加以下代码的原因:

stream = helper.GetStream(content); // to take the stream back and 

然后将其保存到BLOB存储.

这里的GetStream函数具有以下代码:

public static Stream GetStream(string fileContent)
{
    MemoryStream ms = new MemoryStream();
    StreamWriter writer = new StreamWriter(ms);
    writer.WriteLine(fileContent);
    writer.Flush();

    // Rewind the MemoryStream before copying
    ms.Seek(0, SeekOrigin.Begin);
    return ms;
}

在这之后,我被保存的BLOB文件损坏了.

对此有什么帮助吗?

推荐答案

我不确定错误是什么,但将流转换为字符串,然后将该字符串写入新的内存流几乎肯定不是这样做的.

在使用流时需要稍微小心一些,因为并非所有流都是"可查找的",在处理网络流时尤其如此.毕竟,您不能要求服务器随机重新发送字节.

因此,我建议将流复制到内存中,然后将其用于您想要执行的任何操作:

using var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;

using var outputFileStream = new FileStream(filePath2, FileMode.Create);
ms.CopyTo(outputFileStream);
ms.Position = 0;

CloudBlob blob = null;
                await AzureRetryHelper.OperationWithBasicRetryAsync(async () =>
                {
                    blob = await this.blobHelper.UploadFileToBlob(
                    ms,
                    this.config.GetConfigValue(Constants.DataContainerName),
                    pipeline.BlobNativeFilePath,
                    true,
                    pipeline.MimeType).ConfigureAwait(false);
                }).ConfigureAwait(false);

请注意,对于非常大的数据流,这可能会失败.据我所知,由于一个数组可以容纳的元素数量,内存流被限制在2 GB.

Csharp相关问答推荐

从LED面板中提取文本

减少Express Func T、bool断言中的公式中使用的条件运算符(4)数量(最多允许3个)

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

[0-n]范围内有多少个Integer在其小数表示中至少包含一个9?

注册通用工厂的C# Dep注入

Monty Hall游戏节目模拟给我50/50的结果

实现List T,为什么LINQ之后它不会返回MyList?<>(无法强制转换WhereListIterator `1类型的对象)'

从Blob存储中提取tar.gz文件并将提取结果上载到另一个Blob存储

不带身份的Blazor服务器.Net 8 Cookie身份验证

S能够用DATETIME来计算,这有什么错呢?

C#-从基类更新子类

ASP.NET Core AutoMapper:如何解决错误 CS0121调用在以下方法或属性之间不明确

如何使用.NET6WPF打印车票?

如何管理Azure认证客户端响应和证书 fingerprint

委托RequestDelegate不带2个参数-ASP.NET Core 8最小API

RabbitMQ群集的MassTransit配置问题

Blazor:搜索框在第一次搜索时不搜索

JSON串行化程序问题.SQLite中的空值

序列化过程中的死循环

在.Net 8 Visual Studio 2022中启用本机AOT发布时发布失败