Beispiel #1
0
        /// <summary>
        /// Sign method
        /// </summary>
        /// <param name="request"></param>
        /// <param name="clientConfig"></param>
        /// <param name="metrics"></param>
        /// <param name="awsAccessKeyId"></param>
        /// <param name="awsSecretAccessKey"></param>
        public override void Sign(IRequest request, IClientConfig clientConfig, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey)
        {
            base.Sign(request, clientConfig, metrics, awsAccessKeyId, awsSecretAccessKey);
            //内容Md5
            SetContentMd5(request);
            //移除Md5
            //RemoveContentMd5(request);

            //设置Copy头
            SetCopySource(request);

            //设置User-Agent
            SetUserAgent(request);

            //resourcePath
            var resourcePath = GetResourcePath(request);
            //待签名字符串
            var canonicalString = RestUtil.MakeKS3CanonicalString(request.HttpMethod, resourcePath, request, null);

            var hmacBytes = HmacUtil.GetHmacSha1(canonicalString, awsSecretAccessKey);
            //签名
            var signature = Convert.ToBase64String(hmacBytes);

            //默认头部添加
            if (!request.Headers.ContainsKey(Headers.CONTENT_TYPE))
            {
                request.Headers.Add(Headers.CONTENT_TYPE, Headers.DEFAULT_MIMETYPE);
            }
            //如果已经添加了认证,就移除
            if (request.Headers.ContainsKey(Headers.AUTHORIZATION))
            {
                request.Headers.Remove(Headers.AUTHORIZATION);
            }
            request.Headers.Add("Authorization", "KSS " + awsAccessKeyId + ":" + signature);
        }