Skip to content

Instantly share code, notes, and snippets.

@BaileyMillerSSI
Created October 20, 2018 08:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaileyMillerSSI/c8f2e01954fba744525af28e9ed9a088 to your computer and use it in GitHub Desktop.
Save BaileyMillerSSI/c8f2e01954fba744525af28e9ed9a088 to your computer and use it in GitHub Desktop.
[HttpPost("{bucketName}"), DisableRequestSizeLimit]
public async Task<IActionResult> UploadFile([FromRoute]string bucketName)
{
var file = Request.Form.Files[0];
var response = await _service.UploadFileAsync(bucketName, file);
if (response.Status == System.Net.HttpStatusCode.OK)
{
return Ok(response);
}
else
{
return BadRequest(response);
}
}
public async Task<S3Response> UploadFileAsync(String bucketName, IFormFile file)
{
try
{
var transferRequest = new TransferUtilityUploadRequest()
{
InputStream = file.OpenReadStream(),
AutoCloseStream = false,
BucketName = bucketName,
Key = file.FileName,
StorageClass = S3StorageClass.Standard
};
transferRequest.Metadata.Add("Date-UTC-Uploaded", DateTime.UtcNow.ToString());
await new TransferUtility(_client).UploadAsync(transferRequest);
return new S3Response()
{
Message = "File Uploaded",
Status = System.Net.HttpStatusCode.OK
};
}
catch (AmazonS3Exception e)
{
Debug.WriteLine(e.Message);
return new S3Response()
{
Status = System.Net.HttpStatusCode.BadRequest,
Message = e.Message
};
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
return new S3Response()
{
Status = System.Net.HttpStatusCode.BadRequest,
Message = e.Message
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment