加密示例(Node.js SDK)
开发过程中,您有任何问题可以在github上提交issue,或者在华为云对象存储服务论坛中发帖求助。
代码示例一:上传对象加密
本示例使用SSE-C方式加密上传example/objectname到examplebucket桶中。
// 引入obs库
// 使用npm安装
const ObsClient = require("esdk-obs-nodejs");
// 使用源码安装
// var ObsClient = require('./lib/obs');
// 创建ObsClient实例
const obsClient = new ObsClient({
// 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
access_key_id: process.env.ACCESS_KEY_ID,
secret_access_key: process.env.SECRET_ACCESS_KEY,
// 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入
// security_token: process.env.SECURITY_TOKEN,
// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写
server: "https://obs.cn-north-4.myhuaweicloud.com",
});
async function putObject() {
try {
const params = {
// 指定存储桶名称
Bucket: "examplebucket",
// 指定对象名,此处以 example/objectname 为例
Key: "example/objectname",
// 指定文本对象
Body : 'Hello OBS',
// 设置SSE-C算法加密对象
SseC: 'AES256',
SseCKey: 'your sse-c key generated by AES-256 algorithm'
};
// 加密上传对象
const result = await obsClient.putObject(params);
if (result.CommonMsg.Status <= 300) {
console.log("Put object(%s) under the bucket(%s) successful!", params.Bucket);
console.log("RequestId: %s", result.CommonMsg.RequestId);
return;
};
console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
console.log("Status: %d", result.CommonMsg.Status);
console.log("Code: %s", result.CommonMsg.Code);
console.log("Message: %s", result.CommonMsg.Message);
console.log("RequestId: %s", result.CommonMsg.RequestId);
} catch (error) {
console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
console.log(error);
};
};
putObject();
代码示例二:下载对象解密
本示例用于流式下载examplebucket桶中的example/objectname加密对象。
// 引入obs库
// 使用npm安装
const ObsClient = require("esdk-obs-nodejs");
// 使用源码安装
// var ObsClient = require('./lib/obs');
// 创建ObsClient实例
const obsClient = new ObsClient({
// 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
access_key_id: process.env.ACCESS_KEY_ID,
secret_access_key: process.env.SECRET_ACCESS_KEY,
// 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入
// security_token: process.env.SECURITY_TOKEN,
// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写
server: "https://obs.cn-north-4.myhuaweicloud.com",
});
async function getObject() {
try {
const params = {
// 指定存储桶名称
Bucket: "examplebucket",
// 指定对象名,此处以 example/objectname 为例
Key: 'example/objectname',
// 设置SSE-C算法解密对象
SseC: 'AES256',
// 此处的密钥必须和上传对象加密时使用的密钥一致
SseCKey: 'your sse-c key generated by AES-256 algorithm'
};
// 下载加密对象
const result = await obsClient.getObject(params);
if (result.CommonMsg.Status <= 300) {
console.log("Get object(%s) under the bucket(%s) successful!", params.Key, params.Bucket);
console.log("RequestId: %s", result.CommonMsg.RequestId);
console.log('Object Content: %s', result.InterfaceResult.Content);
return;
};
console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
console.log("Status: %d", result.CommonMsg.Status);
console.log("Code: %s", result.CommonMsg.Code);
console.log("Message: %s", result.CommonMsg.Message);
console.log("RequestId: %s", result.CommonMsg.RequestId);
} catch (error) {
console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
console.log(error);
};
};
getObject();