lay out boilerplate for new s3 integration
This commit is contained in:
44
Unbinder/Services/S3Service.cs
Normal file
44
Unbinder/Services/S3Service.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
|
||||
|
||||
Reference in New Issue
Block a user