我一直在尝试使用带有 KMS 密钥的服务器端加密对 AWS S3 进行 PutObject 调用,但调用一直失败并显示 403 代码并且消息显示“访问被拒绝”。
这是我用来拨打电话的 C# 代码:
const string fileName = "test.txt";
using (var stream = new MemoryStream())
{
using (var sw = new StreamWriter(stream))
{
sw.AutoFlush = true;
sw.Write("letsseeifthisworks");
var por = new PutObjectRequest
{
BucketName = _bucketname,
Key = fileName,
InputStream = stream,
ServerSideEncryptionMethod = ServerSideEncryptionMethod.AWSKMS,
ServerSideEncryptionKeyManagementServiceKeyId = _kmsKeyId
};
var response = _s3Client.PutObject(por);
}
}
HTTP 请求如下所示:
PUT https://notarealcorp.s3.us-west-1.amazonaws.com/test.txt HTTP/1.1
Content-Type: text/plain
x-amz-server-side-encryption: aws:kms
x-amz-server-side-encryption-aws-kms-key-id: arn:aws:kms:us-west-1:<account>:key/<kms-key-guid>
User-Agent: aws-sdk-dotnet-45/3.3.16.2 aws-sdk-dotnet-core/3.3.21.6 .NET_Runtime/4.0 .NET_Framework/4.0 OS/Microsoft_Windows_NT_6.1.7601_Service_Pack_1 ClientSync
host: notarealcorp.s3.us-west-1.amazonaws.com
X-Amz-Date: 20180109T072431Z
X-Amz-Decoded-Content-Length: 18
X-Amz-Content-SHA256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD
Authorization: AWS4-HMAC-SHA256 Credential=<access-key>/20180109/us-west-1/s3/aws4_request, SignedHeaders=content-length;content-type;host;user-agent;x-
amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-server-side-encryption;x-amz-server-side-encryption-aws-kms-key-id, Signature=<signature>
Content-Length: 191
Expect: 100-continue
12;chunk-signature=<another-signature>
letsseeifthisworks
0;chunk-signature=<yet-another-signature>
这是回应:
HTTP/1.1 403 Forbidden
x-amz-request-id: <request-id>
x-amz-id-2: <host-id>
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Tue, 09 Jan 2018 07:24:31 GMT
Connection: close
Server: AmazonS3
f3
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message>
<RequestId>request-id</RequestId><HostId>host-id</HostId></Error>
0
这些是我设置的 IAM 权限。首先,S3:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
然后是 KMS:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "kms:*",
"Resource": "*"
}
]
}
当我注释掉加密时,调用通过并且我可以看到正在创建的文件。
我不确定“拒绝访问”是什么意思。我被拒绝访问什么资源?任何帮助将不胜感激。