Server-side encryption

Updated at:

OSS on CloudBox supports server-side encryption. After you enable this feature, objects that you upload to an OSS on CloudBox bucket are automatically encrypted and stored. When you download an object, OSS automatically decrypts the object and includes a header in the HTTP response to indicate that the object was encrypted on the server.

Prerequisites

  • OSS on CloudBox is available only in the China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu) regions.

  • You have purchased a CloudBox.

  • You have created a VPC and a vSwitch for the CloudBox.

  • You have contacted technical support to request a SingleTunnel network type for your CloudBox's VPC.

Encryption methods

OSS on CloudBox uses server-side encryption with OSS-managed keys (SSE-OSS) for encryption and decryption. For added security, OSS also uses a master key to encrypt the data encryption keys. OSS generates and manages data encryption keys and protects them with strong, multi-factor security measures.

The following configuration methods are available:

  • Configure the default encryption method for an OSS on CloudBox bucket

    You can set the default encryption method for an OSS on CloudBox bucket to SSE-OSS and specify the encryption algorithm as AES256. Then, all objects that are uploaded to this bucket are encrypted by default.

  • Configure the encryption method for a specific object

    When you upload an object or modify its metadata, you can include the x-oss-server-side-encryption parameter in the request and set its value to AES256. The target object is then encrypted using an OSS-managed key. For more information, see PutObject.

Important

If you configure a default encryption method for a bucket, you can still specify a different encryption method for an individual object when you upload or copy it. The encryption method specified for the object takes precedence.

Procedure

Use the OSS console

Method 1: Enable server-side encryption when you create a bucket

  1. Log on to the OSS console.

  2. In the navigation pane on the left, choose Data Service > OSS on CloudBox Buckets. Then, click Create Bucket.

  3. On the Create Bucket page, configure the parameters.

    • Encryption Method: Select an encryption method for objects.

      • None: Disables server-side encryption.

      • OSS-Managed: Uses OSS-managed keys for encryption. OSS uses a different key to encrypt each object. For added protection, OSS uses a master key to encrypt the data encryption keys.

    • Encryption Algorithm: Only AES256 is supported.

    For more information about how to configure other parameters, see Create an OSS on CloudBox bucket.

Method 2: Enable server-side encryption for an existing bucket

  1. Log on to the OSS console.

  2. In the navigation pane on the left, choose Data Service > OSS on CloudBox Buckets. Then, in the bucket list, click the name of the target bucket.

  3. In the navigation pane on the left, choose Content Security > Server-Side Encryption.

  4. In the Server-Side Encryption section, click Settings. Configure the following parameters and then click Save.

    • Encryption Method: Select an encryption method for objects.

      • None: Disables server-side encryption.

      • OSS-Managed: Uses OSS-managed keys for encryption. OSS uses a different key to encrypt each object. For added protection, OSS uses a master key to encrypt the data encryption keys.

    • Encryption Algorithm: Only AES256 is supported.

Use an Alibaba Cloud SDK

You can configure server-side encryption by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support.

import com.aliyun.oss.*;
import com.aliyun.oss.model.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // Specify the data endpoint of the OSS on CloudBox bucket.
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the OSS on CloudBox bucket. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the region where the OSS on CloudBox bucket is located.
        String region = "cn-hangzhou";
        // Specify the ID of the CloudBox.
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";        

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release resources.
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .cloudBoxId(cloudBoxId)
                .build();

        try {
            // Set server-side encryption.
            ServerSideEncryptionByDefault applyServerSideEncryptionByDefault = new ServerSideEncryptionByDefault(SSEAlgorithm.AES256);            
            ServerSideEncryptionConfiguration sseConfig = new ServerSideEncryptionConfiguration();
            sseConfig.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
            SetBucketEncryptionRequest request = new SetBucketEncryptionRequest(bucketName, sseConfig);
            ossClient.setBucketEncryption(request);
            System.out.println("Server-side encryption is enabled for the OSS on CloudBox bucket.");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
import alibabacloud_oss_v2 as oss

# Obtain access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the CloudBox bucket is located, such as cn-hangzhou.
cfg.region = "cn-hangzhou"
# Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
cfg.cloud_box_id = "cb-f8z7yvzgwfkl9q0h****"
# Specify the data domain of the CloudBox bucket in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
cfg.endpoint = "cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# Create an OSS client.
client = oss.Client(cfg)

# Set the default server-side encryption method of the CloudBox bucket to SSE-OSS and the encryption algorithm to AES256.
result = client.put_bucket_encryption(oss.PutBucketEncryptionRequest(
    bucket="examplebucket",
    server_side_encryption_rule=oss.ServerSideEncryptionRule(
        apply_server_side_encryption_by_default=oss.ApplyServerSideEncryptionByDefault(
            sse_algorithm="AES256",
        ),
    ),
))
print(f'status code: {result.status_code}, request id: {result.request_id}')
package main

import (
	"context"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

func main() {
	// Use the default configuration of the SDK and obtain access credentials from environment variables.
	// Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		// Specify the region in which the CloudBox bucket is located, such as cn-hangzhou.
		WithRegion("cn-hangzhou").
		// Specify the data domain of the CloudBox bucket in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
		WithEndpoint("cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com").
		// Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
		WithCloudBoxId("cb-f8z7yvzgwfkl9q0h****")

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Set the default server-side encryption method of the CloudBox bucket to SSE-OSS and the encryption algorithm to AES256.
	request := &oss.PutBucketEncryptionRequest{
		Bucket: oss.Ptr("examplebucket"),
		ServerSideEncryptionRule: &oss.ServerSideEncryptionRule{
			&oss.ApplyServerSideEncryptionByDefault{
				SSEAlgorithm: oss.Ptr("AES256"),
			},
		},
	}
	result, err := client.PutBucketEncryption(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket encryption %v", err)
	}
	log.Printf("put bucket encryption result:%#v\n", result)
}

Use ossutil

Method 1: Enable server-side encryption when you create an OSS on CloudBox bucket

For more information about enabling server-side encryption with ossutil when you create an OSS on CloudBox bucket, see bucket-encryption (Server-side encryption).

Method 2: Upload a file and specify an encryption method

For more information about using ossutil to upload a file and specify an encryption method, see Upload a file and specify an encryption method.

Use a REST API

If your program has high customization requirements, you can directly call REST API operations. However, you must manually write code to calculate signatures.

For more information about the API operation for enabling server-side encryption when you create an OSS on CloudBox bucket, see PutBucketEncryption.

For more information about the API operation for specifying an encryption method when you upload a file, see PutObject.