예제 #1
0
        private HttpRequestMessage PreparePutRequest(Stream stream, string filename)
        {
            AwsV4SignatureCalculator calculator     = new AwsV4SignatureCalculator(_secretKey);
            HttpRequestMessage       requestMessage = new HttpRequestMessage(HttpMethod.Put, new Uri($"{_protocol}://{_endpoint}/{_bucketName}/{filename}"));
            DateTime         value = DateTime.UtcNow;
            ByteArrayContent content;

            if (stream is MemoryStream ms)
            {
                content = new ByteArrayContent(ms.ToArray());
            }
            else
            {
                using MemoryStream memoryStream = new MemoryStream();
                stream.CopyTo(memoryStream);
                content = new ByteArrayContent(memoryStream.ToArray());
            }

            requestMessage.Content = content;
            stream.Dispose();

            requestMessage.Headers.Add("Host", _endpoint);
            requestMessage.Headers.Add("X-Amz-Content-Sha256", AwsV4SignatureCalculator.GetPayloadHash(requestMessage));
            requestMessage.Headers.Add("X-Amz-Date", $"{value:yyyyMMddTHHmmssZ}");

            string[] headers    = { "host", "x-amz-content-sha256", "x-amz-date" };
            string   signature  = calculator.CalculateSignature(requestMessage, headers, value);
            string   authHeader = $"AWS4-HMAC-SHA256 Credential={_accessKey}/{value:yyyyMMdd}/us-east-1/s3/aws4_request, SignedHeaders={string.Join(";", headers)}, Signature={signature}";

            requestMessage.Headers.TryAddWithoutValidation("Authorization", authHeader);

            return(requestMessage);
        }
예제 #2
0
        private HttpRequestMessage PrepareGetRequest(string filename)
        {
            AwsV4SignatureCalculator calculator     = new AwsV4SignatureCalculator(_secretKey);
            HttpRequestMessage       requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri($"{_protocol}://{_endpoint}/{_bucketName}/{filename}"));
            DateTime value = DateTime.UtcNow;

            requestMessage.Headers.Add("Host", _endpoint);
            requestMessage.Headers.Add("X-Amz-Content-Sha256", AwsV4SignatureCalculator.GetPayloadHash(requestMessage));
            requestMessage.Headers.Add("X-Amz-Date", $"{value:yyyyMMddTHHmmssZ}");

            string[] headers    = { "host", "x-amz-content-sha256", "x-amz-date" };
            string   signature  = calculator.CalculateSignature(requestMessage, headers, value);
            string   authHeader = $"AWS4-HMAC-SHA256 Credential={_accessKey}/{value:yyyyMMdd}/us-east-1/s3/aws4_request, SignedHeaders={string.Join(";", headers)}, Signature={signature}";

            requestMessage.Headers.TryAddWithoutValidation("Authorization", authHeader);

            return(requestMessage);
        }