Beispiel #1
0
 public S3Exception(string operation, ObjectKey key, S3Error error)
     : base(String.Format("An error occurred accessing S3 {0} {1}: {2}", operation, key, error.Code))
 {
     Operation = operation;
     Key       = key;
     Error     = error;
 }
Beispiel #2
0
        static bool ShouldRetryFromResponse(HttpResponseMessage response)
        {
            if (response.IsSuccessStatusCode)
            {
                return(false);
            }
            S3Error error = DecodeError(response).Result;

            if (error.Code == "RequestTimeout")
            {
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public void ParseS3ErrorXml()
        {
            string xmlText =
                @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Error>
  <Code>NoSuchKey</Code>
  <Message>The resource you requested does not exist</Message>
  <Resource>/mybucket/myfoto.jpg</Resource> 
  <RequestId>4442587FB7D0A2F9</RequestId>
</Error>";
            var error = S3Error.ParseXml(xmlText);

            Assert.Equal("NoSuchKey", error.Code);
            Assert.Equal("The resource you requested does not exist", error.Message);
            Assert.Equal("/mybucket/myfoto.jpg", error.Resource);
            Assert.Equal("4442587FB7D0A2F9", error.RequestId);
        }
Beispiel #4
0
        public void ParseS3Error2()
        {
            string xmlText =
                @"<Error>
	<Code>BadDigest</Code>
	<Message>The Content-MD5 you specified did not match what we received.</Message>
	<RequestId>FE689C8C5E73D8B6</RequestId>
	<CalculatedDigest>1B2M2Y8AsgTpgAmY7PhCfg==</CalculatedDigest>
	<ExpectedDigest>d41d8cd98f00b204e9800998ecf8427e</ExpectedDigest>
	<HostId>3oDR1WldBiVFbMm4EKHrDb4W1haIG5Z8uXYwLuSc/Z1/YWupOApcCtnwmneoAYN4</HostId>
</Error>";
            var error = S3Error.ParseXml(xmlText);

            Assert.Equal("BadDigest", error.Code);
            Assert.Equal("The Content-MD5 you specified did not match what we received.", error.Message);
            Assert.Null(error.Resource);
            Assert.Equal("FE689C8C5E73D8B6", error.RequestId);
        }
Beispiel #5
0
        public async Task PutObject(ObjectKey key, string type, MemoryStream stream, bool compress)
        {
            Stopwatch timer         = Stopwatch.StartNew();
            long      objectLength  = stream.Length;
            long      contentLength = objectLength;
            string    encoding      = null;
            Stream    sourceStream  = stream;

            try
            {
                if (compress)
                {
                    var tempStream = new MemoryStream();
                    using (var compressStream = new GZipStream(tempStream, CompressionMode.Compress, leaveOpen: true))
                    {
                        stream.CopyTo(compressStream);
                    }
                    tempStream.Position = 0;
                    sourceStream        = tempStream;
                    encoding            = "gzip";
                }
                contentLength = sourceStream.Length;

                Func <HttpRequestMessage> request = () => new HttpRequestMessage
                {
                    RequestUri = GetObjectUri(key),
                    Method     = HttpMethod.Put,
                    Headers    =
                    {
                        { "Date",               DateTimeOffset.UtcNow.ToString("r") },
                        { OriginalLengthHeader, objectLength.ToString()             },
                    },
                    Content = new StreamContent(sourceStream)
                    {
                        Headers =
                        {
                            { "Content-Type",     type     },
                            { "Content-Encoding", encoding },
                        },
                    },
                };

                using (HttpResponseMessage response = await SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        S3Error error = await DecodeError(response);

                        LogOperation(
                            "Put",
                            key,
                            type,
                            objectLength,
                            sourceStream.Length,
                            timer,
                            OperationStatus.Error,
                            error.ResponseBody);
                        throw new S3Exception("Put", key, error);
                    }

                    LogOperation("Put", key, type, objectLength, contentLength, timer, OperationStatus.OK, null);
                }
            }
            catch (S3Exception) { throw; }
            catch (Exception e)
            {
                LogOperation(
                    "Put",
                    key,
                    type,
                    objectLength,
                    contentLength,
                    timer,
                    OperationStatus.Exception,
                    e.ToString());
                throw;
            }
        }
Beispiel #6
0
        public async Task <byte[]> GetObject(ObjectKey key)
        {
            Stopwatch timer = Stopwatch.StartNew();
            Func <HttpRequestMessage> request = () => new HttpRequestMessage
            {
                RequestUri = GetObjectUri(key),
                Method     = HttpMethod.Get,
                Headers    = { { "Date", DateTimeOffset.UtcNow.ToString("r") } }
            };

            string contentType   = null;
            long?  contentLength = null;
            long?  objectLength  = null;

            try
            {
                bool         isCompressed;
                MemoryStream responseBuffer;
                using (HttpResponseMessage response = await SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        S3Error error = await DecodeError(response);

                        LogOperation("Get", key, null, null, null, timer, OperationStatus.Error, error.ResponseBody);
                        if (error.Code == "NoSuchKey")
                        {
                            return(null);
                        }
                        if (error.Code == "AccessDenied")
                        {
                            return(null);
                        }
                        throw new S3Exception("Get", key, error);
                    }

                    contentType   = response.Content.Headers.ContentType.ToString();
                    contentLength = response.Content.Headers.ContentLength ?? 0;
                    objectLength  = contentLength;
                    string ol = response.Headers.Value(OriginalLengthHeader);
                    if (!String.IsNullOrWhiteSpace(ol))
                    {
                        objectLength = long.Parse(ol);
                    }

                    responseBuffer = new MemoryStream();
                    await response.Content.CopyToAsync(responseBuffer);

                    responseBuffer.Position = 0;

                    isCompressed = response.Content.Headers.ContentEncoding.Contains("gzip");
                }

                byte[] data = new byte[objectLength.Value];
                if (isCompressed)
                {
                    var sourceStream = new GZipStream(responseBuffer, CompressionMode.Decompress);
                    var targetStream = new MemoryStream(data);
                    sourceStream.CopyTo(targetStream);
                }
                else
                {
                    responseBuffer.Read(data, 0, data.Length);
                }

                LogOperation(
                    "Get",
                    key,
                    contentType,
                    objectLength,
                    contentLength,
                    timer,
                    OperationStatus.OK,
                    null);
                return(data);
            }
            catch (S3Exception) { throw; }
            catch (Exception e)
            {
                LogOperation(
                    "Get",
                    key,
                    contentType,
                    objectLength,
                    contentLength,
                    timer,
                    OperationStatus.Exception,
                    e.ToString());
                throw;
            }
        }