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); }