0

我正在尝试编写一个使用 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 复制命令将记录复制到 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 
4

1 回答 1

0

AFAIK 从 S3 复制数据时,您不必指定 KMS 密钥 ID。master_symmetric_key简单地说,从您的凭据字符串中删除参数。

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

于 2017-07-06T17:10:12.603 回答