lay out boilerplate for new s3 integration

This commit is contained in:
2023-11-17 15:15:55 -06:00
parent aeb3293942
commit c2bd8fac3e
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.Runtime;
namespace Unbinder.Services
{
public class S3Service
{
public static AWSCredentials Credentials =>
new BasicAWSCredentials(
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"),
Environment.GetEnvironmentVariable("AWS_SECRET_KEY"));
public static AmazonS3Client Client => new(Credentials, RegionEndpoint.USEast1);
public async Task<string?> UploadFileAsync(IFormFile file)
{
using IAmazonS3 client = Client;
if (file == null || file.Length == 0)
{
return null;
}
var bucketName = "unbinder-recipe-images";
var keyName = file.FileName;
using var fileTransferUtility = new TransferUtility(client);
await fileTransferUtility.UploadAsync(file.OpenReadStream(), bucketName, keyName);
return keyName;
}
public async Task GetFile(string path, string outFile)
{
using IAmazonS3 client = Client;
var bucketName = "unbinder-recipe-images";
using var fileTransferUtility = new TransferUtility(client);
await fileTransferUtility.DownloadAsync(outFile, bucketName, path);
}
}
}

View File

@@ -9,6 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.300.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" NoWarn="NU1605" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />