对于 AWS C# S3 SDK 使用内存流而不是文件流不将完整文件写入 S3

Using memory stream instead of filestream for AWS C# S3 SDK not writing full file to S3

我有一些代码用于在 AWS S3 存储桶上创建文件。当我使用文件流时,它工作正常并且所有数据都存在。当我使用内存流时,文件中的某些 xml 数据丢失了。这是代码 im 运行:

internal void WriteDataContractToFile(object data, string filename)
    {
        string path = @"C:\Jenova\restfulengine\RESTfulEngine\App_Data\requests\" + filename;
        try
        {
            using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8))
                {
                    DataContractSerializer dcs = new DataContractSerializer(data.GetType());
                    writer.WriteStartDocument();
                    dcs.WriteObject(writer, data);
                }
            }
            var uploadRequest = new TransferUtilityUploadRequest();
            uploadRequest.FilePath = path;
            uploadRequest.Key = filename;
            uploadRequest.BucketName = bucketName;
            transferUtility.Upload(uploadRequest);

            //METHOD WITH MEMORY STREAM THAT DOESNT WORK

            using (MemoryStream stream = new MemoryStream())
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8))
                {
                    DataContractSerializer dcs = new DataContractSerializer(data.GetType());
                    writer.WriteStartDocument();
                    dcs.WriteObject(writer, data);
                    var uploadRequest1 = new TransferUtilityUploadRequest();
                    uploadRequest1.InputStream = stream;
                    uploadRequest1.Key = filename;
                    uploadRequest1.BucketName = bucketName;
                    transferUtility.Upload(uploadRequest);
                }
            }
        }
        
    }

当我对文件流使用第一种方法时,所有数据都存在于我在 S3 中创建的文件中。当我使用内存流时,我错过了文件的结尾:

并且缺少文件结尾:

它应该是这样的:

有人看到我做错了什么吗?我不明白为什么内存流会截断数据。即使我将该内存流写入本地文件以测试数据也不完整,所以我有点不知所措,感谢任何建议!

您在流完全写入之前上传。可能最后一个缓冲区是在处理写入器时写入的。

所以:

using (MemoryStream stream = new MemoryStream())
{
    using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8))
    {
        DataContractSerializer dcs = new DataContractSerializer(data.GetType());
        writer.WriteStartDocument();
        dcs.WriteObject(writer, data);
    }

    var uploadRequest1 = new TransferUtilityUploadRequest();
    uploadRequest1.InputStream = stream;
    uploadRequest1.Key = filename;
    uploadRequest1.BucketName = bucketName;
    transferUtility.Upload(uploadRequest);
}

对最后一个参数使用 XmlDictionaryWriter.CreateTextWriter(Stream, Encoding, Boolean)false 以使其保持流打开。