Skip to content

Instantly share code, notes, and snippets.

@ZhenDeng
Created February 14, 2024 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhenDeng/69f502c95033bf0ebd6cf4645d356303 to your computer and use it in GitHub Desktop.
Save ZhenDeng/69f502c95033bf0ebd6cf4645d356303 to your computer and use it in GitHub Desktop.
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