• Object Storage Service

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

Example of Encryption

Encrypting an Object to Be Uploaded

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

PutObjectRequest request = new PutObjectRequest();
request.setBucketName("bucketname");
request.setObjectKey("objectkey");
request.setFile(new File("localfile"));
// Set the SSE-C encryption algorithm.
SseCHeader sseCHeader = new SseCHeader();
sseCHeader.setAlgorithm(ServerAlgorithm.AES256);
sseCHeader.setSseCKey("your sse-c key generated by AES-256 algorithm");

request.setSseCHeader(sseCHeader);
obsClient.putObject(request);

// Close obsClient. 
obsClient.close();

Decrypting a Downloaded Object

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectkey");
// Set the encryption algorithm to SSE-C.
SseCHeader sseCHeader = new SseCHeader();
sseCHeader.setAlgorithm(ServerAlgorithm.AES256);
// The key used here must be the one used for uploading the object.
sseCHeader.setSseCKey("your sse-c key generated by AES-256 algorithm");

request.setSseCHeader(sseCHeader);
ObsObject obsObject = obsClient.getObject(request);

obsObject.getObjectContent().close();

// Close obsClient. 
obsClient.close();