Client-side encryption (Java SDK V2)

Updated at:

Use the client-side encryption feature of OSS Java SDK V2 to encrypt data locally before you upload it. Your data stays secure in transit and at rest.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the com.aliyun:alibabacloud-oss-v2 dependency to your pom.xml file.

  • Access credentials are configured by using environment variables. The sample code in this topic uses EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles under the account have no permissions until the Alibaba Cloud account or an account administrator grants them the required permissions.

API

Action

Description

PutObject (client-side encryption)

oss:PutObject

Uploads an object by using client-side encryption.

Sample code

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.encryption.EncryptionConfiguration;
import com.aliyun.sdk.service.oss2.encryption.MasterCipher;
import com.aliyun.sdk.service.oss2.encryption.OSSEncryptionClient;
import com.aliyun.sdk.service.oss2.encryption.RsaMasterCipher;
import com.aliyun.sdk.service.oss2.models.GetObjectRequest;
import com.aliyun.sdk.service.oss2.models.GetObjectResult;
import com.aliyun.sdk.service.oss2.models.PutObjectRequest;
import com.aliyun.sdk.service.oss2.models.PutObjectResult;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class ClientEncryptionSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "exampleobject";

        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
        keyGenerator.initialize(2048);
        KeyPair keyPair = keyGenerator.generateKeyPair();
        MasterCipher masterCipher = RsaMasterCipher.of(keyPair, null);
        EncryptionConfiguration encryptionConfiguration = EncryptionConfiguration.newBuilder()
                .masterCipher(masterCipher)
                .build();

        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(new EnvironmentVariableCredentialsProvider())
                .region(region)
                .build();

        try (OSSEncryptionClient encryptionClient =
                     new OSSEncryptionClient(client, encryptionConfiguration)) {
            PutObjectResult putResult = encryptionClient.putObject(PutObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .body(BinaryData.fromString("hello world - encrypted content"))
                    .build());
            System.out.printf("PutObject status code:%d, request id:%s%n",
                    putResult.statusCode(), putResult.requestId());

            try (GetObjectResult getResult = encryptionClient.getObject(
                    GetObjectRequest.newBuilder().bucket(bucket).key(key).build())) {
                System.out.printf("GetObject status code:%d%n", getResult.statusCode());
            }
        }
    }
}

References