【发布时间】:2017-11-22 15:29:22
【问题描述】:
我尝试使用以下参数验证 AWS S3 队列: 访问密钥、密钥、队列 URL
public bool ValidateSqs(string queue_url, string access_key, string secret_key)
{
if (string.IsNullOrWhiteSpace(access_key)
|| string.IsNullOrWhiteSpace(secret_key)
|| string.IsNullOrWhiteSpace(queue_url))
return false;
try
{
SqsClient sqsValid = new SqsClient(access_key, secret_key, queue_url);
return sqsValid.CheckAwsCredentials();
}
catch (Exception ex)
{
LogUtils.Error("fail to validate SQS", ex);
return false;
}
}
这里是 SqsClient:
public class SqsClient
{
private AmazonSQSClient _awsSQSClient;
private readonly int MaxRetryAttempts = 3;
public SqsClient(string accessKey, string secretKey, string url)
{
if (string.IsNullOrEmpty(accessKey))
{
throw new ArgumentNullException("accessKey");
}
if (string.IsNullOrEmpty(secretKey))
{
throw new ArgumentNullException("secretKey");
}
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
AccessKey = accessKey;
SecretKey = secretKey;
Url = url;
try
{
RestartClient();
}
catch (Exception ex)
{
LogUtils.WithParameters()
.Append(x => AccessKey)
.Append(x => SecretKey)
.Error("Failed to create SQS Client", ex);
throw;
}
}
public bool CheckAwsCredentials()
{
try
{
_awsSQSClient.GetQueueAttributes(new GetQueueAttributesRequest
{
QueueUrl = Url,
AttributeNames = new List<string> { "All" }
});
return true;
}
catch (Exception ex)
{
LogUtils.Error(ex);
return false;
}
}
异常是从 CheckAwsCredentials() 方法引发的。
Amazon.SQS.AmazonSQSException:我们计算的请求签名 与您提供的签名不匹配。检查您的 AWS 秘密 访问密钥和签名方法。查阅服务文档以获取 详情。
此请求的规范字符串应该是 'POST /49512474474/DEX队列
内容类型:应用程序/x-www-form-urlencoded;字符集=utf-8 主机:sqs.ap-southeast-2.amazonaws.com 用户代理:aws-sdk-dotnet-45/3.3.2.4 aws-sdk-dotnet-core/3.3.8.1 .NET_Runtime/4.0 .NET_Framework/4.0 操作系统/Microsoft_Windows_NT_10.0.14393.0 ClientSync x-amz-content-sha256:92f6fc97d2a609de283521acc0f05e5fabe54f4afe7427217ef2fc5521 x-amz-日期:20170620T072209Z
内容类型;主机;用户代理;x-amz-content-sha256;x-amz-date 92f6fc97d2a609d5e283521acc0bbccf05e5fabe54f4afe7427217ef2fc5521'
错误的原因可能是什么?
感谢您的帮助!
【问题讨论】:
-
最可能的原因是由于复制错误,
secretKey的值不正确。 -
找到了!我有一个转义方法,将我的密钥中的 char '+' 转换为 ' '!谢谢@Michael-sqlbot
标签: c# .net amazon-web-services amazon-s3