iognrngngnioretniret

This commit is contained in:
2024-11-08 14:44:52 +09:00
parent 008365566e
commit 8d6d0c6517

View File

@@ -4,6 +4,7 @@ using System.IO.Compression;
using System.Net; using System.Net;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommunityToolkit.HighPerformance;
using K4os.Compression.LZ4.Streams; using K4os.Compression.LZ4.Streams;
using Minio; using Minio;
using Minio.DataModel; using Minio.DataModel;
@@ -11,155 +12,31 @@ using Minio.DataModel.Args;
using Minio.DataModel.Encryption; using Minio.DataModel.Encryption;
using Minio.Exceptions; using Minio.Exceptions;
class Program namespace Ttalkkak
{ {
static async Task Main(string[] args) static class Program
{ {
//Console.WriteLine("Hello, World!"); static async Task Main(string[] args)
//Uri uri = new Uri("https://a4plane.duckdns.org/minio/");
Uri url = new Uri("http://a4plane.duckdns.org/a4plane/djdp");
//Uri url = new Uri("https://github.com/netdata/netdata");
using (var httpClient = new HttpClient())
{ {
var content = await httpClient.GetStringAsync(url); IMinioClient client = new MinioClient()
Console.Write(content); .WithEndpoint("a4plane.duckdns.org", 8000)
}; .WithCredentials("1234", "12345678")
// Minio 설정 (Minio 클라이언트 인스턴스 생성) .WithSSL()
//IWebProxy proxy = new WebProxy("https://a4plane.duckdns.org/minio/", 443); .Build();
//IMinioClient minioClient = new MinioClient() try
// .WithEndpoint(uri)
// .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}' 폴더가 존재하지 않습니다."); var args2 = new GetObjectArgs()
return; .WithBucket("testob")
.WithObject("testo.txt")
.WithFile("testo.txt");
;
await client.GetObjectAsync(args2 );
} }
catch (MinioException e)
if (File.Exists(tempZipFile))
{ {
Console.WriteLine($"경고: '{tempZipFile}' 파일이 이미 존재합니다. 덮어씁니다."); Console.WriteLine(e);
File.Delete(tempZipFile); throw;
}
// 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);
}
}
}