使用KMS Managed CMK时,复制到redshift失败并显示错误

时间:2017-07-05 19:15:52

标签: java amazon-redshift aws-kms amazon-kms

我正在尝试编写一个使用KMS密钥id加密数据的java程序。我使用默认的java代码将对象上传到S3。我正在将要上传到S3的值更改为记录,以便我可以将其加载到红移。

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import junit.framework.Assert;

import org.apache.commons.io.IOUtils;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3EncryptionClient;
import com.amazonaws.services.s3.model.CryptoConfiguration;
import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

public class testKMSkeyUploadObject {

    private static AmazonS3EncryptionClient encryptionClient;

    public static void main(String[] args) throws Exception { 
       String bucketName = "***bucket name***"; 
        String objectKey  = "ExampleKMSEncryptedObject";
        String kms_cmk_id = "***AWS KMS customer master key ID***";

        KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(kms_cmk_id);

        encryptionClient = new AmazonS3EncryptionClient(new ProfileCredentialsProvider(), materialProvider,
                new CryptoConfiguration().withKmsRegion(Regions.US_EAST_1))
            .withRegion(Region.getRegion(Regions.US_EAST_1));

        // Upload object using the encryption client.
        byte[] plaintext = "xyz,abc,1"
                .getBytes();
        System.out.println("plaintext's length: " + plaintext.length);
        encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey,
                new ByteArrayInputStream(plaintext), new ObjectMetadata()));

     // Download the object.
        S3Object downloadedObject = encryptionClient.getObject(bucketName,
                objectKey);
        byte[] decrypted = IOUtils.toByteArray(downloadedObject
                .getObjectContent());

        // Verify same data.
        Assert.assertTrue(Arrays.equals(plaintext, decrypted));
    }
}

我正在使用具有以下语法的Redhsift copy命令将记录复制到redshift。

copy table_name from 's3://bucket-name/KMSEncryptedObject' credentials as
'aws_access_key_id=<access-key-id>;aws_secret_access_key=<secret-access-key>;master_symmetric_key=<master-key>'

当在复制命令上面运行时,我遇到以下错误:

Query execution failed 
Reason: SQL Error [500310] [XX000]: [Amazon](500310) Invalid operation: Failed writing body (0 != 16) 
Cause: S3 object 'KMSEncyptedObjecr does not have 'x-amz-meta-x-amz-key metadata 
Details: 
----------------
error: Failed writing body (0 != 16) Cause: S3 object 'KMSEncyptedObjeci does not have 'x-amz-meta-x-amz-key metadata 

1 个答案:

答案 0 :(得分:0)

AFAIK在从S3复制数据时,您不必指定KMS密钥ID。只需从您的凭据字符串中删除master_symmetric_key参数即可。

查看http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html您只需要上传KMS密钥ID(当然,IAM用户/角色需要有权访问此密钥)。