コード例 #1
0
ファイル: PutObjectTests.cs プロジェクト: yeurch/aws-sdk-net
        private static void Test(int errorSize, bool failRequest)
        {
            var actualSize = errorSize + 128;
            //var data = new string('@', actualSize + bufferSize);
            //byte[] bytes = Encoding.UTF8.GetBytes(data);
            var bytes = CreateData(actualSize);

            // must precompute this and set in headers to avoid hash computation on ErrorStream
            // affecting the test
            var payloadhash = UtilityMethods.ToHex(UtilityMethods.ComputeSHA256(bytes), true);

            ErrorStream es = ErrorStream.Create(bytes);

            if (failRequest)
            {
                es.MaxReadBytes = errorSize;
            }
            // 1 rewind for S3 pre-marshallers which reset position to 0
            // 1 rewind for exception at error size
            es.MinRewinds = 2;

            var putRequest = new PutObjectRequest
            {
                BucketName      = bucketName,
                Key             = "foo1",
                AutoCloseStream = false
            };

            putRequest.Headers["x-amz-content-sha256"] = payloadhash;
            putRequest.InputStream = es;

            CallWithTimeout(() => Client.PutObject(putRequest), TimeSpan.FromSeconds(10));
        }
コード例 #2
0
        public void TestStreamRetry2()
        {
            string data = "sample data";

            byte[] bytes = Encoding.UTF8.GetBytes(data);

            // must precompute this and set in headers to avoid hash computation on ErrorStream
            // affecting the test
            var         payloadhash = UtilityMethods.ToHex(UtilityMethods.ComputeSHA256(bytes), true);
            ErrorStream es          = ErrorStream.Create(bytes, readOnly: true);

            es.MaxReadBytes = 3;
            int requestCount = 0;

            es.OnRead += (s, e) =>
            {
                if (++requestCount == 2)
                {
                    throw new WebException("Fake WebException", WebExceptionStatus.KeepAliveFailure);
                }
            };
            var putRequest = new PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = "foo",
                InputStream = es,
                Headers     = { ContentLength = data.Length },
            };

            putRequest.Headers["x-amz-content-sha256"] = payloadhash;

            var exception = AssertExtensions.ExpectException <AmazonServiceException>(() => Client.PutObject(putRequest));

            es = ErrorStream.Create(bytes, readOnly: true);
            es.MaxReadBytes = 3;
            putRequest      = new PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = "foo",
                InputStream = es,
                Headers     = { ContentLength = data.Length },
            };
            putRequest.Headers["X-Amz-Content-SHA256"] = payloadhash;
            Client.PutObject(putRequest);

            string responseData;

            using (var responseStream = Client.GetObject(new GetObjectRequest
            {
                BucketName = bucketName,
                Key = putRequest.Key
            }).ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    responseData = reader.ReadToEnd();
                }

            Assert.AreEqual(data, responseData);
        }
コード例 #3
0
        public void TestStreamRetry1()
        {
            string data = "sample data";

            byte[] bytes = Encoding.UTF8.GetBytes(data);
            // must precompute this and set in headers to avoid hash computation on ErrorStream
            // affecting the test
            var         payloadhash = UtilityMethods.ToHex(UtilityMethods.ComputeSHA256(bytes), true);
            ErrorStream es          = ErrorStream.Create(bytes);

            es.MaxReadBytes = 3;
            int requestCount = 0;

            es.OnRead += (s, e) =>
            {
                if (++requestCount == 2)
                {
                    throw new IOException("Fake Exception");
                }
            };
            var putRequest = new PutObjectRequest
            {
                BucketName      = bucketName,
                Key             = "foo1",
                InputStream     = es,
                AutoCloseStream = false
            };

            putRequest.Headers["x-amz-content-sha256"] = payloadhash;
            Client.PutObject(putRequest);

            string responseData;

            using (var responseStream = Client.GetObject(new GetObjectRequest
            {
                BucketName = bucketName,
                Key = putRequest.Key
            }).ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    responseData = reader.ReadToEnd();
                }

            Assert.AreEqual(data, responseData);

            requestCount           = 0;
            putRequest.InputStream = es;
            Client.PutObject(putRequest);

            TestStreamRetry2();
        }