首页 > 解决方案 > 使用 s3Boto 进行服务器端加密 - 使用 KMS 托管密钥进行服务器端加密需要 HTTP 标头 x-amz-server-side-encryption : aws:kms

问题描述

我正在尝试在我的 django 应用程序上设置服务器端加密以进行文件上传。我正在使用 s3Boto3Storage。我找不到有关如何实现服务器端加密的明确文档,并且在尝试上传文件时,出现以下错误:

An error occurred (InvalidArgument) when calling the PutObject operation: Server Side Encryption with KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms

这是我的设置的样子:

AWS_ACCESS_KEY_ID = 'XXXX'
AWS_SECRET_ACCESS_KEY = 'XXXX'
AWS_STORAGE_BUCKET_NAME = 'tickets'
AWS_S3_ENDPOINT_URL = 'https://sfo2.digitaloceanspaces.com'
AWS_S3_FILE_OVERWRITE = False
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = ''
AWS_DEFAULT_ACL = None
AWS_S3_ENCRYPTION = True

STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

标签: djangoamazon-s3boto3django-storage

解决方案


我相信这告诉您,您需要在此处的某处包含 'x-amz-server-side-encryption : aws:kms' 标头 - 可能在您的 AWS_S3_OBJECT_PARAMETERS 字典中。

如果您尝试使用自定义 CMK 进行加密,则需要像这样构建其他标头:

"ServerSideEncryption": "aws:kms" # This value instructs the request to use a CMK for 
    # server-side encryption, and requires you to pass your custom CMK id to the value
    # for the next param, 'SSEKMSKeyId'.
    # The other acceptable value is AES256, which uses the AWS S3 SSE to encrypt, and not a CMK.
"SSEKMSKeyId": "<your kms cmk key id goes here>" # This is the id of your custom CMK. 
    # This is not required if you set "ServerSideEncryption": "AES256" above.

相关文档,我一开始也没有得到:

x-amz-server-side-encryption 将此对象存储在 S3 中时使用的服务器端加密算法(例如,AES256、aws:kms)。有效值:AES256 | aws:公里

x-amz-server-side-encryption-aws-kms-key-id 如果存在 x-amz-server-side-encryption 且值为 aws:kms,则此标头指定 AWS Key Management Service 的 ID (AWS KMS) 用于对象的主加密密钥。

如果 x-amz-server-side-encryption 的值为 aws:kms,则此标头指定将用于对象的 AWS Key Management Service (AWS KMS) 主加密密钥的 ID。如果您指定 x-amz-server-side-encryption:aws:kms,但不提供 x-amz-server-side-encryption-aws-kms-key-id,则 Amazon S3 使用默认 AWS KMS 密钥来保护数据。

S3 PUT 文档在这里:https ://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html


推荐阅读