Created
February 14, 2024 09:38
-
-
Save ZhenDeng/69f502c95033bf0ebd6cf4645d356303 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Threading.Tasks; | |
using Amazon.S3; | |
using Amazon.S3.Model; | |
namespace S3Operations | |
{ | |
class CreateObjectTask | |
{ | |
public async Task Run() | |
{ | |
Console.WriteLine("\nStart of create object task"); | |
Console.WriteLine("\nReading configuration for bucket name..."); | |
var configSettings = ConfigSettingsReader<S3ConfigSettings>.Read("S3"); | |
try | |
{ | |
using var s3Client = new AmazonS3Client(); | |
// Create object in the Amazon S3 bucket | |
await UploadObject(s3Client, | |
configSettings.BucketName, | |
$"{configSettings.ObjectName}{configSettings.SourceFileExtension}", | |
configSettings.SourceContentType, | |
new Dictionary<string, string> | |
{ | |
{ configSettings.MetadataKey, configSettings.MetadataValue } | |
}); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"Error: {e.Message}"); | |
} | |
Console.WriteLine("\nEnd of create object task"); | |
} | |
async Task UploadObject(IAmazonS3 s3Client, | |
string bucketName, | |
string name, | |
string contentType, | |
IDictionary<string, string> metadata) | |
{ | |
Console.WriteLine("Creating object..."); | |
// Start TODO 5: create a object by transferring the file to the S3 bucket, | |
// set the contentType of the file and add any metadata passed to this function. | |
var request = new PutObjectRequest | |
{ | |
BucketName = bucketName, | |
Key = name, | |
FilePath = System.IO.Path.Join(Environment.CurrentDirectory, name), | |
ContentType = contentType | |
}; | |
foreach (var key in metadata.Keys) | |
{ | |
request.Metadata.Add(key, metadata[key]); | |
} | |
await s3Client.PutObjectAsync(request); | |
// End TODO 5 | |
Console.WriteLine("Finished creating object"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment