简体   繁体   中英

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

I have some code that is meant to create a file on AWS S3 bucket. When I use a file stream it works fine and all the data is present. When I use a memory stream, some of the xml data in the file is missing. Here is the code im running:

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

When I use the first method with the file stream all data is present in the file I create in S3. When I use the memory stream, I am missing the end of the file:

缺少结束标签

And is missing the end of the file:

在此处输入图像描述

This is what it is supposed to look like:

它应该是什么

Does anyone see anything I am doing wrong? It doesnt make sense to me why the memory stream is truncating the data. Even when I write that memory stream to a file locally for testing the data is not complete so I am kind of at a loss, any suggestions are appreciated!

You're uploading before the stream is fully written. Probably the last buffer is written when you dispose the writer.

So:

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

Use XmlDictionaryWriter.CreateTextWriter(Stream, Encoding, Boolean) with false for the last parameter to let it keep the stream open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM