Commented S3 code and assemble in diffenrent function for better reading
This commit is contained in:
parent
194aacb232
commit
907fa27180
|
@ -683,17 +683,21 @@ internal static class Program
|
||||||
|
|
||||||
private static async Task<Boolean> UploadCsv(StatusRecord status, DateTime timeStamp)
|
private static async Task<Boolean> UploadCsv(StatusRecord status, DateTime timeStamp)
|
||||||
{
|
{
|
||||||
var s3Config = status.Config.S3;
|
var csv = status.ToCsv().LogInfo();
|
||||||
var csv = status.ToCsv().LogInfo();
|
|
||||||
|
|
||||||
// This is temporary for Wittman, but now it's for all Installation
|
await RestApiSavingfile(csv);
|
||||||
//await File.WriteAllTextAsync("/var/www/html/status.csv", csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines());
|
|
||||||
|
var s3Config = status.Config.S3;
|
||||||
|
|
||||||
if (s3Config is null)
|
if (s3Config is null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//this for concatenating in one file
|
//Concatenating 15 files in one file
|
||||||
|
return await ConcatinatingAndCompressingFiles(timeStamp, s3Config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<Boolean> ConcatinatingAndCompressingFiles(DateTime timeStamp, S3Config s3Config)
|
||||||
|
{
|
||||||
if (_counterOfFile >= NbrOfFileToConcatenate)
|
if (_counterOfFile >= NbrOfFileToConcatenate)
|
||||||
{
|
{
|
||||||
_counterOfFile = 0;
|
_counterOfFile = 0;
|
||||||
|
@ -701,6 +705,8 @@ internal static class Program
|
||||||
var logFileConcatenator = new LogFileConcatenator();
|
var logFileConcatenator = new LogFileConcatenator();
|
||||||
var csvToSend = logFileConcatenator.ConcatenateFiles(NbrOfFileToConcatenate);
|
var csvToSend = logFileConcatenator.ConcatenateFiles(NbrOfFileToConcatenate);
|
||||||
|
|
||||||
|
File.WriteAllText("test.csv",csvToSend);
|
||||||
|
|
||||||
var s3Path = timeStamp.ToUnixTime() + ".csv";
|
var s3Path = timeStamp.ToUnixTime() + ".csv";
|
||||||
var request = s3Config.CreatePutRequest(s3Path);
|
var request = s3Config.CreatePutRequest(s3Path);
|
||||||
|
|
||||||
|
@ -708,24 +714,7 @@ internal static class Program
|
||||||
|
|
||||||
//Use this for no compression
|
//Use this for no compression
|
||||||
//var response = await request.PutAsync(new StringContent(csv));
|
//var response = await request.PutAsync(new StringContent(csv));
|
||||||
|
var compressedBytes = CompresseBytes(csvToSend);
|
||||||
//Compress CSV data to a byte array
|
|
||||||
byte[] compressedBytes;
|
|
||||||
using (var memoryStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
//Create a zip directory and put the compressed file inside
|
|
||||||
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
|
||||||
{
|
|
||||||
var entry = archive.CreateEntry("data.csv", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
|
|
||||||
using (var entryStream = entry.Open())
|
|
||||||
using (var writer = new StreamWriter(entryStream))
|
|
||||||
{
|
|
||||||
writer.Write(csvToSend);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compressedBytes = memoryStream.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode the compressed byte array as a Base64 string
|
// Encode the compressed byte array as a Base64 string
|
||||||
string base64String = Convert.ToBase64String(compressedBytes);
|
string base64String = Convert.ToBase64String(compressedBytes);
|
||||||
|
@ -749,6 +738,45 @@ internal static class Program
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Byte[] CompresseBytes(String csvToSend)
|
||||||
|
{
|
||||||
|
//Compress CSV data to a byte array
|
||||||
|
byte[] compressedBytes;
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
//Create a zip directory and put the compressed file inside
|
||||||
|
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
||||||
|
{
|
||||||
|
var entry = archive.CreateEntry("data.csv", CompressionLevel.SmallestSize); // Add CSV data to the ZIP archive
|
||||||
|
using (var entryStream = entry.Open())
|
||||||
|
using (var writer = new StreamWriter(entryStream))
|
||||||
|
{
|
||||||
|
writer.Write(csvToSend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
compressedBytes = memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return compressedBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task RestApiSavingfile(String csv)
|
||||||
|
{
|
||||||
|
// This is for the Rest API
|
||||||
|
// Check if the directory exists, and create it if it doesn't
|
||||||
|
const String directoryPath = "/var/www/html";
|
||||||
|
|
||||||
|
if (!Directory.Exists(directoryPath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directoryPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
string filePath = Path.Combine(directoryPath, "status.csv");
|
||||||
|
|
||||||
|
await File.WriteAllTextAsync(filePath, csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines());
|
||||||
|
}
|
||||||
|
|
||||||
private static Boolean IsPowerOfTwo(Int32 n)
|
private static Boolean IsPowerOfTwo(Int32 n)
|
||||||
{
|
{
|
||||||
return n > 0 && (n & (n - 1)) == 0;
|
return n > 0 && (n & (n - 1)) == 0;
|
||||||
|
|
|
@ -64,7 +64,7 @@ public record S3Config
|
||||||
{
|
{
|
||||||
|
|
||||||
contentType = "application/base64; charset=utf-8";
|
contentType = "application/base64; charset=utf-8";
|
||||||
//contentType = "text/plain; charset=utf-8";
|
//contentType = "text/plain; charset=utf-8"; //this to use when sending plain csv to S3
|
||||||
|
|
||||||
var payload = $"{method}\n{md5Hash}\n{contentType}\n{date}\n/{bucket.Trim('/')}/{s3Path.Trim('/')}";
|
var payload = $"{method}\n{md5Hash}\n{contentType}\n{date}\n/{bucket.Trim('/')}/{s3Path.Trim('/')}";
|
||||||
using var hmacSha1 = new HMACSHA1(UTF8.GetBytes(s3Secret));
|
using var hmacSha1 = new HMACSHA1(UTF8.GetBytes(s3Secret));
|
||||||
|
|
Loading…
Reference in New Issue