• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP SDK)
  4. Server-Side Encryption
  5. Example of Encryption

Example of Encryption

Encrypting an Object to Be Uploaded

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );
    
$resp = $obsClient->putObject ( [ 
       'Bucket' => 'bucketname',
       'Key' => 'objectkey',
       'SourceFile' => 'localfile', 
        // Set the SSE-C encryption algorithm.  
       'SseC' => 'AES256',       
       'SseCKey' => 'your sse-c key generated by AES-256 algorithm'
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

$resp = $obsClient->putObject ( [ 
       'Bucket' => 'bucketname',
       'Key' => 'objectkey2',
       'SourceFile' => 'localfile2',    
       // Set the SSE-KMS encryption algorithm.   
       'SseKms' => 'aws:kms'
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

Decrypting a Downloaded Object

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->getObject ( [ 
       'Bucket' => 'bucketname',
       'Key' => 'objectkey',   
       // Set the SSE-C decryption algorithm.    
       'SseC' => 'AES256',
        // The key used here must be the one used for uploading the object.
       'SseCKey' => 'your sse-c key generated by AES-256 algorithm'
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );
printf("Object Content:\n");
// Obtain the object content.
echo $resp ['Body'];