Configuring Bucket Encryption (SDK for C)
If you have any questions during development, post them on the Issues page of GitHub.
Function
If encryption is enabled for a bucket, objects uploaded to it will be encrypted based on the specified encryption method before being stored in OBS. When you download these objects, OBS decrypts them first before returning them to you.
This API is used to configure or update encryption for a bucket.
Restrictions
- To configure encryption for a bucket, you must be the bucket owner or have the required permission (obs:bucket:PutEncryptionConfiguration granted using IAM or PutEncryptionConfiguration granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
Method
void set_bucket_encryption(const obs_options *options,
obs_server_side_encryption_configuration *encryption_config,
obs_response_handler *handler, void *callback_data); Request Parameters
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| options | const obs_options* | Yes | Explanation: Context of the requested bucket. You can set the AK, SK, endpoint, bucket, timeout interval, and temporary credentials through obs_options. Restrictions: None Value range: None Default value: None |
| encryption_config | Table 2 * | Yes | Explanation: Bucket encryption configuration structure. Restrictions: None Value range: None Default value: None |
| handler | Yes | Explanation: A callback structure where all members are pointers to callback functions, used to set the callback functions that handle response data. You can set a callback function to copy the response data from the server to callback_data (custom callback data). Restrictions: None Value range: None Default value: None | |
| callback_data | void * | No | Explanation: Custom callback data. Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| rule | Table 3 * | Yes | Explanation: Server-side encryption rule. Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| sse_algorithm | char * | Yes | Explanation: Server-side encryption algorithm. Restrictions: None Value range:
Default value: None |
| kms_data_encryption | char * | No | Explanation: Encryption algorithm used in SSE-KMS mode. Restrictions: This parameter is valid only when sse_algorithm is set to kms. Value range: SM4 Default value: If this parameter is not specified, AES256 is used by default. |
| kms_master_key_id | char * | No | Explanation: ID of the KMS master key used in SSE-KMS mode. Restrictions: This parameter is valid only when sse_algorithm is set to kms. If this parameter is not specified, the default master key is used. Value range: None Default value: None |
| project_id | char * | No | Explanation: ID of the project where the KMS master key belongs in SSE-KMS mode. Restrictions: This parameter is valid only when sse_algorithm is set to kms. If this parameter is not specified, the default project ID is used. Value range: None Default value: None |
Sample Code
This example configures SSE-OBS for a bucket.
#include "eSDKOBS.h"
#include <stdio.h>
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
int main()
{
obs_initialize(OBS_INIT_ALL);
obs_options options;
init_obs_options(&options);
// Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables for identity authentication. Before running the code in this example, configure local environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
options.bucket_options.bucket_name = "example-bucket-name";
// Configure an encryption rule.
obs_server_side_encryption_rule rule = {0};
rule.sse_algorithm = "AES256";
// Set the encryption configuration.
obs_server_side_encryption_configuration encryption_config = {0};
encryption_config.rule = &rule;
obs_response_handler response_handler = {&response_properties_callback, &response_complete_callback};
obs_status ret_status = OBS_STATUS_BUTT;
set_bucket_encryption(&options, &encryption_config, &response_handler, &ret_status);
if (OBS_STATUS_OK == ret_status) {
printf("set bucket encryption successfully.\n");
} else {
printf("set bucket encryption failed(%s).\n", obs_get_status_name(ret_status));
}
obs_deinitialize();
}
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
(void)properties; (void)callback_data;
return OBS_STATUS_OK;
}
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
if (callback_data) { *(obs_status*)callback_data = status; }
if (error && error->message) { printf("Error: %s\n", error->message); }
} Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot