Using Client-Side Encryption for Amazon S3 in the AWS SDK for PHP

Using Client-Side Encryption for Amazon S3 in the AWS SDK for PHP

Setup

Encryption

 
 // Let's construct our S3EncryptionClient using an S3Client
 $encryptionClient = new S3EncryptionClient(
  new S3Client([
   'region' => 'us-east-1',
   'version' => 'latest',
  ])
 );

 $kmsKeyArn = 'arn-to-the-kms-key';
 // This materials provider handles generating a cipher key and initialization
 // vector, as well as encrypting your cipher key via AWS KMS
 $materialsProvider = new KmsMaterialsProvider(
  new KmsClient([
   'region' => 'us-east-1',
   'version' => 'latest',
  ]),
  $kmsKeyArn
 );

 $bucket = 'the-bucket-name';
 $key = 'the-upload-key';
 $cipherOptions = [
  'Cipher' => 'gcm'
  'KeySize' => 256,
  // Additional configuration options
 ];

 $result = $encryptionClient->putObject([
  '@MaterialsProvider' => $materialsProvider,
  '@CipherOptions' => $cipherOptions,
  'Bucket' => $bucket,
  'Key' => $key,
  'Body' => fopen('file-to-encrypt.txt'),
 ]);
 

Decryption

  $result = $encryptionClient->getObject([  '@MaterialsProvider' => $materialsProvider,  '@CipherOptions' => [   // Additional configuration options  ],   'Bucket' => $bucket,  'Key' => $key, ]); 

Comments

Popular posts from this blog

A Tailwind CSS Preset for Laravel 5.5

Short and safe array iteration

PHPStorm's performance