Skip to content

Instantly share code, notes, and snippets.

@ShawInnes
Last active April 21, 2019 05:48
Show Gist options
  • Save ShawInnes/ebe1b0da3f305267c325e9451f1cf9de to your computer and use it in GitHub Desktop.
Save ShawInnes/ebe1b0da3f305267c325e9451f1cf9de to your computer and use it in GitHub Desktop.
Using AWS Services in .net core console application
namespace Project
{
public static partial class Program
{
private static readonly AmazonS3Client _s3Client;
private static async Task Main(string[] args)
{
var profileName = "awsprofile"; // from ~/.aws/credentials
var chain = new CredentialProfileStoreChain();
if (chain.TryGetAWSCredentials(profileName, out var awsCredentials)
&& chain.TryGetProfile(profileName, out var awsProfile))
{
_s3Client = new AmazonS3Client(awsCredentials, awsProfile.Region);
}
var key = "filename.txt";
var fileContent = "File Content Goes Here";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent)))
{
await _s3Client.PutObjectAsync(new PutObjectRequest
{
BucketName = BucketName,
Key = key,
AutoCloseStream = true,
ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
StorageClass = S3StorageClass.Standard,
InputStream = stream, ContentType = "application/json"
});
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.3.100.5" />
<PackageReference Include="AWSSDK.S3" Version="3.3.101.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment