Using s3 server encryption with PHP

I decided to use hassle on the new server-side encryption with s3, however I ran into a problem that I cannot solve.

I am using the PHP s3 class found here: https://github.com/tpyo/amazon-s3-php-class

I used this code to initially place objects (and it worked):

     S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
         array(),
    array( 
        "Content-Disposition" => "attachment; filename=$filename",
        "Content-Type" => "application/octet-stream"
            )
);

      

Then I did the following: http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectPUT.html and added ' x-amz-server-side -encryption '. But now when I try to place the object, it crashes without error.

My new code:

  S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
     array(),
array( 
    "Content-Disposition" => "attachment; filename=$filename",
    "Content-Type" => "application/octet-stream",
        "x-amz-server-side​-encryption" => "AES256"
        )

      

);

Has anyone experimented with this new feature or might someone see a bug in the code.

Greetings.

+2


source to share


4 answers


This header must be part of an array $metaHeaders

, not $requestHeaders

.

S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
     array(
        "x-amz-server-side​-encryption" => "AES256"
     ),
     array( 
       "Content-Disposition" => "attachment; filename=$filename",
       "Content-Type" => "application/octet-stream"
     )
);

      

Here's the method definition from the docs :



putObject (mixed $input, 
           string $bucket, 
           string $uri, 
           [constant $acl = S3::ACL_PRIVATE], 
           [array $metaHeaders = array()], 
           [array $requestHeaders = array()])

      


Can you also consider using the PHP SDK ?

+1


source


We can upload files using encryption using the following code: $s3->create_object($bucket_name,$destination,array( 'acl'=>AmazonS3::ACL_PUBLIC, 'fileUpload' => $file_local, 'encryption'=>"AES256"));



And you can download the latest sdk from here

0


source


With the official SDK:

use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = '*** Your File Path ***';

// Instantiate the client.
$s3 = S3Client::factory();

// Upload a file with server-side encryption.
$result = $s3->putObject(array(
    'Bucket'               => $bucket,
    'Key'                  => $keyname,
    'SourceFile'           => $filepath,
    'ServerSideEncryption' => 'AES256',
));

      

Changing the server-side encryption of an existing object (copy operation)

use Aws\S3\S3Client;

$sourceBucket = '*** Your Source Bucket Name ***';
$sourceKeyname = '*** Your Source Object Key ***';

$targetBucket = '*** Your Target Bucket Name ***';
$targetKeyname = '*** Your Target Object Key ***';

// Instantiate the client.
$s3 = S3Client::factory();

// Copy an object and add server-side encryption.
$result = $s3->copyObject(array(
    'Bucket'               => $targetBucket,
    'Key'                  => $targetKeyname,
    'CopySource'           => "{$sourceBucket}/{$sourceKeyname}",
    'ServerSideEncryption' => 'AES256',
));

      

Source: http://docs.aws.amazon.com/AmazonS3/latest/dev/SSEUsingPHPSDK.html

0


source


With laravel 5+ this can be done easily with config.php filesystem, you don't need to get driver or low level object.

's3' => [
  'driver' => 's3',
  'key'    => "Your Key",
  'secret' => "Your Secret",
  'region' => "Bucket Region",
  'bucket' => "Bucket Name",
  'options' => [
    'ServerSideEncryption' => 'AES256',
  ]
],
//Code
$disk->put("filename", "content", "public"); // will have AES for file

      

0


source







All Articles