Created
May 12, 2022 05:52
-
-
Save nikolaymatrosov/7a4fe7c72e353e1047b92240e9ecb6a6 to your computer and use it in GitHub Desktop.
Yandex Cloud Object Storage Transfer Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Amazon.S3; | |
using Amazon.S3.Transfer; | |
namespace ConsoleApp1 | |
{ | |
static class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
string? awsAccessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"); | |
string? awsSecretKey = Environment.GetEnvironmentVariable("AWS_SECRET_KEY"); | |
string? bucket = Environment.GetEnvironmentVariable("BUCKET"); | |
Debug.Assert(awsAccessKey != null, "Missing AWS_ACCESS_KEY env variable"); | |
Debug.Assert(awsSecretKey != null, "Missing AWS_SECRET_KEY env variable"); | |
Debug.Assert(bucket != null, "Missing BUCKET env variable"); | |
AmazonS3Config configsS3 = new AmazonS3Config | |
{ | |
ServiceURL = "https://s3.yandexcloud.net" | |
}; | |
AmazonS3Client s3Client = new AmazonS3Client( | |
awsAccessKey, | |
awsSecretKey, | |
configsS3 | |
); | |
var storageClass = S3StorageClass.Standard; | |
var keyName = "data"; | |
var contentType = "application/octet-stream"; | |
var partSize = 6 * 1024 * 1024; // 6 MB. | |
await using (var input = new StreamReader("/data.txt").BaseStream) | |
{ | |
var fileTransferUtility = new TransferUtility(s3Client); | |
void DisplayProgress(object? sender, UploadProgressArgs args) | |
{ | |
Console.WriteLine(args); | |
} | |
var fileTransferUtilityRequest = new TransferUtilityUploadRequest | |
{ | |
BucketName = bucket, | |
InputStream = input, | |
AutoCloseStream = true, | |
StorageClass = storageClass ?? S3StorageClass.IntelligentTiering, | |
PartSize = partSize, | |
Key = keyName, | |
CannedACL = S3CannedACL.PublicRead, | |
ContentType = contentType, | |
}; | |
fileTransferUtilityRequest.UploadProgressEvent += DisplayProgress; | |
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment