public async Task <IActionResult> Post(IFormFile file, string objectKey) { var fileName = Path.GetFileName( ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"')); if (string.IsNullOrWhiteSpace(objectKey)) { objectKey = S3KeyGenerator.GenerateObjectKey(fileName); } using (var stream = file.OpenReadStream()) { ImageInfo info = ImageProcessor.GetImageInfo(stream); ImageUploadedModel model = await ImageStore.UploadImage( S3Settings.OriginalBucketName, S3Settings.OriginalBucketUrl, objectKey, S3StorageClass.Standard, S3CannedACL.Private, S3Settings.VaultName, info); if (model.Exception != null) { Logger.LogError("An error occured while uploading to S3", model.Exception); return(StatusCode((int)HttpStatusCode.InternalServerError)); } return(Created(model.ObjectLocation, model)); } }
public async Task <ImageUploadedModel> UploadImageAsync(string bucketName, string objectKey, string filePath) { using (var transferUtility = new TransferUtility(_s3Client, _config)) { var model = new ImageUploadedModel(); var request = new TransferUtilityUploadRequest { Key = objectKey, BucketName = _settings.OriginalBucketName, StorageClass = S3StorageClass.Standard, CannedACL = S3CannedACL.Private, FilePath = filePath, }; await transferUtility.UploadAsync(request); return(model); } }
public async Task <IActionResult> Get(string originalKey, int?width, int?height, string versionId) { if (string.IsNullOrWhiteSpace(versionId)) { versionId = await ImageStore.GetLatestVersionId(S3Settings.OriginalBucketName, originalKey); if (string.IsNullOrWhiteSpace(versionId)) { // this image doesn't exist return(NotFound()); } } string resizedKey = S3KeyGenerator.GenerateObjectKeyWithSize(originalKey, width, height, versionId); // check if the resized image exists if (await ImageStore.ImageExists(S3Settings.ResizedBucketName, resizedKey)) { return(Redirect(S3Settings.ResizedBucketUrl + resizedKey)); } Stream responseStream = await ImageStore.GetImage(S3Settings.OriginalBucketName, originalKey, versionId); // resize the image ImageInfo info = ImageProcessor.Resize(responseStream, width, height); ImageUploadedModel model = await ImageStore.UploadImage( S3Settings.ResizedBucketName, S3Settings.ResizedBucketUrl, resizedKey, S3StorageClass.ReducedRedundancy, S3CannedACL.PublicRead, null, info); if (model.Exception != null) { Logger.LogError("An error occured while uploading to S3", model.Exception); return(StatusCode((int)HttpStatusCode.InternalServerError)); } return(Redirect(S3Settings.ResizedBucketUrl + resizedKey)); }
public async Task <IActionResult> RestoreArchive(string jobId) { GetJobOutputRequest request = new GetJobOutputRequest { VaultName = S3Settings.VaultName, JobId = jobId }; var response = GlacierClient.GetJobOutput(request); ArchiveDescription description = JsonConvert.DeserializeObject <ArchiveDescription>(response.ArchiveDescription); // AWS HashStream doesn't support seeking so we need to copy it back to a MemoryStream MemoryStream outputStream = new MemoryStream(); response.Body.CopyTo(outputStream); ImageUploadedModel model = await ImageStore.UploadImage( S3Settings.OriginalBucketName, S3Settings.OriginalBucketUrl, description.ObjectKey, S3StorageClass.StandardInfrequentAccess, S3CannedACL.Private, null, new ImageInfo { MimeType = description.ContentType, Width = description.Width, Height = description.Height, Image = outputStream } ); return Created(model.ObjectLocation, model); }
public async Task <ImageUploadedModel> UploadImage( string bucketName, string bucketUrl, string objectKey, S3StorageClass storageClass, S3CannedACL permissions, string glacierVaultName, ImageInfo image) { ImageUploadedModel model = new ImageUploadedModel(); try { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = objectKey, StorageClass = storageClass, CannedACL = permissions, ContentType = image.MimeType, AutoCloseStream = false }; putRequest.Metadata.Add("width", image.Width.ToString()); putRequest.Metadata.Add("height", image.Height.ToString()); putRequest.InputStream = image.Image; byte[] md5Hash = image.Image.Md5Hash(); putRequest.MD5Digest = md5Hash.ToBase64String(); PutObjectResponse response = await S3Client.PutObjectAsync(putRequest); string eTag = response.ETag.Trim('"').ToLowerInvariant(); string expectedETag = md5Hash.ToS3ETagString(); if (eTag != expectedETag) { throw new Exception("The eTag received from S3 doesn't match the eTag computed before uploading. This usually indicates that the image has been corrupted in transit."); } // upload to Glacier if needed if (!string.IsNullOrWhiteSpace(glacierVaultName)) { ArchiveDescription description = new ArchiveDescription { ObjectKey = objectKey, ContentType = image.MimeType, Width = image.Width, Height = image.Height }; // reset stream position in image image.Image.Position = 0; UploadArchiveRequest glacierRequest = new UploadArchiveRequest { ArchiveDescription = JsonConvert.SerializeObject(description, Formatting.None), Body = image.Image, VaultName = glacierVaultName, Checksum = TreeHashGenerator.CalculateTreeHash(image.Image) }; UploadArchiveResponse glacierResponse = await GlacierClient.UploadArchiveAsync(glacierRequest); model.ArchiveId = glacierResponse.ArchiveId; } model.ObjectKey = objectKey; model.ETag = eTag; model.ObjectLocation = bucketUrl + objectKey; model.VersionId = response.VersionId; } catch (Exception ex) { model.Exception = ex; } return(model); }