7. 使用https

NOS Node.js SDK支持使用https的方式调用相关的接口,以保证安全性

7.1. 使用https

NOS Node.js SDK默认使用http协议,若需使用https协议,只需在初始化NosClient实例时设置传输协议和客户端证书路径,代码如下:

var nosclient = new NosClient();
nosclient.setAccessId('您的accessKeyId');
nosclient.setSecretKey('您的accessKeySecret');
nosclient.setEndpoint('建桶时选择的的区域域名');
nosclient.setProtocol('https');
nosclient.setCaPath('您提供的证书的路径');
nosclient.setPort('443');

Attention

  1. 客户端也可不配置证书,NOS Node.js SDK会忽略客户端证书验证。
  2. NOS Node.js SDK默认使用http协议,也可以通过nosclient.setProtocol(‘http’);设置http协议。

7.2. 实例

以下代码实现以https的方式上传本地文件,具体实现如下:

var nosclient = new NosClient();
nosclient.setAccessId('您的accessKeyId');
nosclient.setSecretKey('您的accessKeySecret');
nosclient.setEndpoint('建桶时选择的的区域域名');
nosclient.setProtocol('https');
nosclient.setCaPath('您提供的证书的路径');
nosclient.setPort('443');

var map = {
    bucket: 'bucketName', //桶名
    key: 'objectName', //对象名
    filepath: 'path' //文件路径(包含文件名)
};
var cb = function(result) {
    console.log(result);
};

try {
    nosclient.put_file(map, cb);
}
catch(err) {
    console.log("Failed with code:" + err.code);
}