I am trying to encrypt a pdf file in my application with a key I created in AWS IAM and upload the encrypted file to S3. I use boto3 to achieve this. I could upload the file to S3 without encryption though. Here is my function that does the encryption :
def write(self):
print 'Write to S3'
client = boto3.client('kms')
s3 = boto3.client('s3')
input_file = open('265987747.pdf', 'rb')
data = input_file.read()
input_file.close()
print type(data)
response = client.encrypt(
KeyId='alias/efax',
Plaintext=data,
EncryptionContext={
'string': 'string'
}
)
#Upload file to S3
#s3.upload_file("265987747.pdf", "bucket_efax", "265987747.pdf")
I get this following error :
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Encrypt operation: 1 validation error detected: Value at 'plaintext' failed to satisfy constraint: Member must have length less than or equal to 4096
I am not sure if I am using the correct method to encrypt a file in KMS.