Created
October 20, 2018 08:21
-
-
Save BaileyMillerSSI/c8f2e01954fba744525af28e9ed9a088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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