Skip to content

Instantly share code, notes, and snippets.

@awswithdotnet
Created April 3, 2022 14:52
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 awswithdotnet/c363f345e4eabbef89ae6d76043f912f to your computer and use it in GitHub Desktop.
Save awswithdotnet/c363f345e4eabbef89ae6d76043f912f to your computer and use it in GitHub Desktop.
S3 Presigned Api UploadController Complete
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Api.Models;
using Microsoft.AspNetCore.Mvc;
namespace Api.Controllers;
[ApiController]
[Route("[controller]")]
public class UploadController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Post(CreateSignedUrlRequest createSignedUrlRequest)
{
string bucketName = "";
string key = Guid.NewGuid().ToString() + ".txt";
RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
var client = new AmazonS3Client(bucketRegion);
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = key,
ContentBody = createSignedUrlRequest.Content
};
PutObjectResponse putObjectResponse = await client.PutObjectAsync(putRequest);
GetPreSignedUrlRequest preSignedUrlRequest = new GetPreSignedUrlRequest
{
BucketName = bucketName,
Key = key,
Expires = DateTime.UtcNow.AddHours(createSignedUrlRequest.TimeToLiveInHours)
};
string preSignedUrl = client.GetPreSignedURL(preSignedUrlRequest);
return Ok(preSignedUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment