Esempio n. 1
0
        internal override void Execute()
        {
            FileStream input = File.OpenRead(filePath);

            string checksum = TreeHashGenerator.CalculateTreeHash(input);

            try
            {
                UploadArchiveRequest uploadRequest = new UploadArchiveRequest()
                {
                    AccountId          = this.options.AccountId,
                    ArchiveDescription = archiveDescription,
                    VaultName          = vaultName,
                    Checksum           = checksum,
                    Body = input
                };

                uploadRequest.StreamTransferProgress += this.ProgressCallback;
                uploadRequest.BeforeRequestEvent     += new UserAgentPostFix("SingleUpload").UserAgentRequestEventHandlerSync;

                UploadArchiveResponse uploadArchivResponse =
                    this.manager.GlacierClient.UploadArchive(uploadRequest);
                string archiveId = uploadArchivResponse.ArchiveId;
                this.UploadResult = new UploadResult(archiveId, checksum);
            }
            finally
            {
                try { input.Close(); }
                catch (Exception) { }
            }
        }
        internal override async Task ExecuteAsync()
        {
            FileStream input = File.OpenRead(filePath);

            string checksum = TreeHashGenerator.CalculateTreeHash(input);

            try
            {
                UploadArchiveRequest uploadRequest = new UploadArchiveRequest()
                {
                    AccountId          = this.options.AccountId,
                    ArchiveDescription = archiveDescription,
                    VaultName          = vaultName,
                    Checksum           = checksum,
                    Body = input
                };

                uploadRequest.StreamTransferProgress += this.ProgressCallback;
                ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)uploadRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("SingleUpload").UserAgentRequestEventHandlerSync);

                UploadArchiveResponse uploadArchivResponse =
                    await this.manager.GlacierClient.UploadArchiveAsync(uploadRequest).ConfigureAwait(false);

                string archiveId = uploadArchivResponse.ArchiveId;
                this.UploadResult = new UploadResult(archiveId, checksum);
            }
            finally
            {
                try { input.Dispose(); }
                catch (Exception) { }
            }
        }
Esempio n. 3
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            UploadArchiveResponse response = new UploadArchiveResponse();

            context.Read();

            UnmarshallResult(context, response);
            return(response);
        }
Esempio n. 4
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            UploadArchiveResponse response = new UploadArchiveResponse();

            context.Read();

            response.UploadArchiveResult = UploadArchiveResultUnmarshaller.GetInstance().Unmarshall(context);

            return(response);
        }
Esempio n. 5
0
 private static void UnmarshallResult(JsonUnmarshallerContext context, UploadArchiveResponse response)
 {
     if (context.ResponseData.GetHeaderValue("Location") != null)
     {
         response.Location = context.ResponseData.GetHeaderValue("Location");
     }
     if (context.ResponseData.GetHeaderValue("x-amz-sha256-tree-hash") != null)
     {
         response.Checksum = context.ResponseData.GetHeaderValue("x-amz-sha256-tree-hash");
     }
     if (context.ResponseData.GetHeaderValue("x-amz-archive-id") != null)
     {
         response.ArchiveId = context.ResponseData.GetHeaderValue("x-amz-archive-id");
     }
     return;
 }
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            UploadArchiveResponse response = new UploadArchiveResponse();


            if (context.ResponseData.GetHeaderValue("Location") != null)
            {
                response.Location = context.ResponseData.GetHeaderValue("Location");
            }
            if (context.ResponseData.GetHeaderValue("x-amz-sha256-tree-hash") != null)
            {
                response.Checksum = context.ResponseData.GetHeaderValue("x-amz-sha256-tree-hash");
            }
            if (context.ResponseData.GetHeaderValue("x-amz-archive-id") != null)
            {
                response.ArchiveId = context.ResponseData.GetHeaderValue("x-amz-archive-id");
            }
            return(response);
        }
Esempio n. 7
0
        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);
        }