我正在尝试将json格式的数据保存到s3,但根本没有在磁盘上保存文件。
我用流作家创建了字符串。然后我用https://github.com/icsharpcode/SharpZipLib压缩字节数组 然后将其上传到AWS S3。问题是,如果我查看s3上的文件,我会看到它是filename.json.gz,这是正确的,但是当我下载它时,由于某种原因它会将它下载为filename.zip
这是我的代码:
public UploadToS3Response BackupEventsToS3WithoutFiles(int tripId, string profileId, DateTimeOffset eventsReceivedAt, ICollection<AppComponents.Models.Event> events, string filePath, string s3Path)
{
var response = new UploadToS3Response() { Success = false };
var fileName = $"tripevents-{tripId}-{DateTimeOffset.Now.ToString("yyyyMMdd")}-{profileId}";
var s3PathPrefix = s3Path;
try
{
var accessKey = ConfigurationManager.AppSettings["aws:accesskey"];
var accessSecret = ConfigurationManager.AppSettings["aws:secretkey"];
var zippedStream = ZipBytes(fileName,events,tripId);
byte[] zipData = Encoding.ASCII.GetBytes(zippedStream.ToString());
var uploadToS3Response = Helpers.AWSHelper.UploadFiletoS3fromZip(accessKey, accessSecret, zipData, $"{fileName}.json.gz", s3PathPrefix, FileGranularity.Hourly, eventsReceivedAt);
if (uploadToS3Response.Success)
{
response.Success = true;
response.S3FilePath = uploadToS3Response.S3FilePath;
}
}
catch (Exception ex)
{
ex.ToExceptionless().Submit();
throw;
}
return response;
}
这是我的邮编班
public byte[] ZipBytes(string filename, ICollection<AppComponents.Models.Event> events, int tripId)
{
var fullFileName = $"{filename}.json.gz";
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
byte[] bytes = null;
var newEntry = new ZipEntry($"{filename}.json.gz");
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
bytes = WriteEventsToJsonBytes(events, tripId);
MemoryStream inStream = new MemoryStream(bytes);
StreamUtils.Copy(inStream, zipStream, new byte[4096]);
inStream.Close();
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
return outputMemStream.ToArray();
}
这是我的WriteEventsToJsonBytes
private byte[] WriteEventsToJsonBytes(ICollection<AppComponents.Models.Event> events, int tripId)
{
StringBuilder builder = new StringBuilder();
var row = "";
foreach (var eventData in events)
{
eventData.TripId = tripId;
JObject obj = JObject.FromObject(eventData);
row = obj.ToString(Newtonsoft.Json.Formatting.None);
builder.Append(row);
row = "";
}
// convert string to stream
byte[] byteArray = Encoding.ASCII.GetBytes(builder.ToString());
return byteArray;
}
任何帮助将不胜感激