157 lines
5.6 KiB
C#
157 lines
5.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Threading.Tasks;
|
|
using K4os.Compression.LZ4.Streams;
|
|
using Minio;
|
|
using Minio.DataModel;
|
|
using Minio.DataModel.Args;
|
|
using Minio.DataModel.Encryption;
|
|
using Minio.Exceptions;
|
|
|
|
class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
//Console.WriteLine("Hello, World!");
|
|
|
|
// Minio 설정 (Minio 클라이언트 인스턴스 생성)
|
|
IWebProxy proxy = new WebProxy("https://a4plane.duckdns.org", 443);
|
|
IMinioClient minioClient = new MinioClient()
|
|
.WithEndpoint("a4plane.duckdns.org")
|
|
.WithCredentials("a4plane", "661434Ghkd")
|
|
.WithSSL()
|
|
.WithProxy(proxy)
|
|
.Build();
|
|
|
|
// 압축 및 업로드할 폴더 및 파일 경로 설정
|
|
string sourceFolder = "testo";
|
|
string bucketName = "testob";
|
|
string objectName = "testtt.lz4";
|
|
string outputLz4File = Path.Combine(Directory.GetCurrentDirectory(), objectName);
|
|
|
|
// 폴더를 압축
|
|
CompressFolderWithProgress(sourceFolder, outputLz4File);
|
|
|
|
// 압축한 파일을 Minio로 업로드
|
|
await UploadWithProgress((MinioClient)minioClient, bucketName, objectName, outputLz4File);
|
|
}
|
|
|
|
static void CompressFolderWithProgress(string sourceFolder, string outputLz4File)
|
|
{
|
|
string tempZipFile = Path.GetTempFileName();
|
|
try
|
|
{
|
|
// 압축 전 폴더 확인
|
|
if (!Directory.Exists(sourceFolder))
|
|
{
|
|
Console.WriteLine($"오류: '{sourceFolder}' 폴더가 존재하지 않습니다.");
|
|
return;
|
|
}
|
|
|
|
if (File.Exists(tempZipFile))
|
|
{
|
|
Console.WriteLine($"경고: '{tempZipFile}' 파일이 이미 존재합니다. 덮어씁니다.");
|
|
File.Delete(tempZipFile);
|
|
}
|
|
|
|
// ZIP 파일 압축
|
|
ZipFile.CreateFromDirectory(sourceFolder, tempZipFile, CompressionLevel.Fastest, false);
|
|
Console.WriteLine("ZIP 압축 완료");
|
|
|
|
// ZIP -> LZ4 압축
|
|
var zipFileInfo = new FileInfo(tempZipFile);
|
|
long zipFileSize = zipFileInfo.Length;
|
|
long processedSize = 0;
|
|
|
|
using var zipFileStream = File.OpenRead(tempZipFile);
|
|
using var outputFileStream = File.Create(outputLz4File);
|
|
using var lz4Stream = LZ4Stream.Encode(outputFileStream);
|
|
|
|
var buffer = new byte[8192];
|
|
int bytesRead;
|
|
while ((bytesRead = zipFileStream.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
lz4Stream.Write(buffer, 0, bytesRead);
|
|
processedSize += bytesRead;
|
|
int progress = (int)(100 * processedSize / zipFileSize);
|
|
Console.Write($"\rLZ4 압축 진행도: {progress}%");
|
|
}
|
|
|
|
Console.WriteLine("\nLZ4 압축 완료");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"오류 발생: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(tempZipFile))
|
|
{
|
|
File.Delete(tempZipFile);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 폴더의 전체 파일 크기를 계산하는 메서드
|
|
static long CalculateFolderSize(string folder)
|
|
{
|
|
long size = 0;
|
|
foreach (var filePath in Directory.GetFiles(folder, "*", SearchOption.AllDirectories))
|
|
{
|
|
var fileInfo = new FileInfo(filePath);
|
|
size += fileInfo.Length;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
static async Task UploadWithProgress(MinioClient minio, string bucketName, string objectName, string filePath)
|
|
{
|
|
try
|
|
{
|
|
Aes aesEncryption = Aes.Create();
|
|
aesEncryption.KeySize = 256;
|
|
aesEncryption.GenerateKey();
|
|
var ssec = new SSEC(aesEncryption.Key);
|
|
var progress = new Progress<ProgressReport>(progressReport =>
|
|
{
|
|
// Progress events are delivered asynchronously (see remark below)
|
|
Console.WriteLine(
|
|
$"Percentage: {progressReport.Percentage}% TotalBytesTransferred: {progressReport.TotalBytesTransferred} bytes");
|
|
if (progressReport.Percentage != 100)
|
|
Console.SetCursorPosition(0, Console.CursorTop - 1);
|
|
else Console.WriteLine();
|
|
});
|
|
|
|
var putObjectArgs = new PutObjectArgs()
|
|
.WithBucket(bucketName)
|
|
.WithObject(objectName)
|
|
.WithFileName(filePath)
|
|
.WithContentType("application/octet-stream")
|
|
.WithServerSideEncryption(ssec)
|
|
.WithProgress(progress);
|
|
if (putObjectArgs == null)
|
|
{
|
|
Console.WriteLine("putObjectArgs is null.");
|
|
return; // 혹은 예외를 던지는 방법으로 조치
|
|
}
|
|
if (minio == null)
|
|
{
|
|
Console.WriteLine("MinioClient is null.");
|
|
}
|
|
Console.WriteLine($"Bucket Name: {bucketName}, Object Name: {objectName}, File Path: {filePath}");
|
|
|
|
|
|
await minio.PutObjectAsync(putObjectArgs);
|
|
Console.WriteLine($"{objectName} is uploaded successfully.");
|
|
}
|
|
catch (MinioException e)
|
|
{
|
|
Console.WriteLine("Error occurred: " + e);
|
|
}
|
|
}
|
|
}
|