• Object Storage Service

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

Example of Encryption

Encrypting an Object to Be Uploaded

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.put_object_header import PutObjectHeader
from com.obs.models.server_side_encryption import SseCHeader, SseKmsHeader
headers = PutObjectHeader()
# Set the SSE-C encryption algorithm.
headers.sseHeader = SseCHeader(encryption='AES256', key='your sse-c key generated by AES-256 algorithm')

resp = obsClient.putFile('bucketname', 'objectkey', 'localfile', headers=headers)

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)

headers = PutObjectHeader()
# Set the SSE-KMS encryption algorithm.
headers.sseHeader = SseKmsHeader.getInstance()
resp = obsClient.putFile('bucketname', 'objectkey2', 'localfile2', headers=headers2)

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)

Decrypting a Downloaded Object

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.get_object_header import GetObjectHeader
from com.obs.models.server_side_encryption import SseCHeader
headers = GetObjectHeader()
# Set the SSE-C decryption algorithm. The key used here must be the one used for uploading the object.
headers.sseHeader = SseCHeader(encryption='AES256', key='your sse-c key generated by AES-256 algorithm')
resp = obsClient.getObject('bucketname', 'objectkey', 'localfile', headers=headers)

if resp.status < 300:    
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)    
    print('errorMessage:', resp.errorMessage)