我的C#项目用于执行两个操作:
- 下载SharePoint文件并
- 通过将下载的流保存到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文件损坏了.
对此有什么帮助吗?