Skip to content

Instantly share code, notes, and snippets.

@bradirby
Created June 3, 2022 09:57
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 bradirby/5bd847965fdc760bc86f57a9d1f9af52 to your computer and use it in GitHub Desktop.
Save bradirby/5bd847965fdc760bc86f57a9d1f9af52 to your computer and use it in GitHub Desktop.
Service and Test to read and write from S3 using C#
using System;
using System.IO;
using System.Text.Json;
using System.Text;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3.Model;
using Amazon.S3;
using NUnit.Framework;
namespace S3Test
{
internal class S3IntegrationGist
{
[Test]
[Explicit]
public async Task WriteToS3()
{
string bucketname = "bradsnewbucket";
//key name is the file name of the bucket item
string keyName = $"my_test_class_instance";
var awsAccessKey = Environment.GetEnvironmentVariable("S3AccessKey");
var awsSecretKey = Environment.GetEnvironmentVariable("S3SecretKey");
var svc = new S3Service(awsAccessKey, awsSecretKey, RegionEndpoint.USEast1);
var usr = await svc.ReadFromS3Async<MyTestClass>(bucketname, keyName);
if (usr == null)
{
usr = new MyTestClass
{
FirstName = "Brad",
LastName = "Irby"
};
}
else
{
usr.LastName = Guid.NewGuid().ToString();
}
await svc.WriteToS3Async(usr, bucketname, keyName);
}
internal class MyTestClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
public class S3Service
{
private AmazonS3Client Client { get; set; }
public S3Service(string accessKey, string secretKey, RegionEndpoint regionEndpoint)
{
Client = new AmazonS3Client(accessKey, secretKey, regionEndpoint);
}
/// <summary>
/// Read from an S3 Bucket
/// </summary>
/// <param name="bucketname">This is the Access Point Alias for the bucket</param>
/// <param name="keyName">This is the name of the object to retrieve, prefaced by folder name if necessary. No leading slash</param>
/// <returns>The object requested or Null if no object exists</returns>
public async Task<T> ReadFromS3Async<T>(string bucketname, string keyName) where T: class
{
try
{
var req = new GetObjectRequest
{
BucketName = bucketname,
Key = keyName
};
var ms = new MemoryStream();
using (var getObjectResponse = await Client.GetObjectAsync(req))
{
await getObjectResponse.ResponseStream.CopyToAsync(ms);
}
ms.Position = 0;
var s3Str = await new StreamReader(ms).ReadToEndAsync();
var newObj = JsonSerializer.Deserialize<T>(s3Str);
return newObj;
}
catch (AmazonS3Exception awsExc) when (awsExc.ErrorCode == "NoSuchKey")
{
return null;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
/// <summary>
/// Writes to an S3 Bucket. This will overwrite existing objects with no warning.
/// </summary>
/// <param name="bucketname">This is the Access Point Alias for the bucket</param>
/// <param name="keyName">This is the name of the object to retrieve, prefaced by folder name if necessary. No leading slash</param>
/// <returns>The object requested or Null if no object exists</returns>
public async Task WriteToS3Async<T>(T obj, string bucketname, string keyName)
{
try
{
var objStr = JsonSerializer.Serialize(obj);
byte[] memstring = new ASCIIEncoding().GetBytes(objStr);
using (var memStream = new MemoryStream(objStr.Length))
{
memStream.Write(memstring, 0, memstring.Length);
using (var tranUtility = new Amazon.S3.Transfer.TransferUtility(Client))
{
await tranUtility.UploadAsync(memStream, bucketname, keyName);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment