Overview
The OSS SDK allows you to integrate SDKs that are used to upload data to and download data from OSS. In practice, you may need to use API operations to upload and download data with a signature. This topic uses the PutObject API as an example to describe how to upload and download data with Node.js.
Note
Note: We recommend that you use the OSS SDK. This topic provides an example of how to use a signature to upload objects. You can modify the business code in actual use.
Detail
The Node.js V12.10.0 code for implementing PutObject is as follows:
Note
Note: The request module contains the crypto-js and request modules.
var HmacSha1 = require('crypto-js/hmac-sha1') ;
var Base64 = require('crypto-js/enc-base64');
var request = require("request");
//endpoint
var endpont = 'http://oss-cn-hangzhou.aliyuncs.com';
//bucket
var bucketName = 'xxx';
//objectname
var objectname= 'mytest/nodejs.txt';
//accesskey
var accesskey = 'xxxx';
//accesskeySecret
var accesskeysecret ='xxx';
// Generate the GMT time
var gmtDate = (new Date()).toGMTString();
console.log(gmtDate);
var contenttype = 'application/json';
var strtosgin = 'PUT\n\n'+contenttype+'\n'+gmtDate+'\n'+'/'+bucketName+'/'+objectname
var sign = Base64.stringify((HmacSha1(strtosgin,accesskeysecret)));
var options = {
url: 'http://xxx.oss-cn-hangzhou.aliyuncs.com/'+objectname,
headers: {
'Content-Type':contenttype,
'Date':gmtDate,
'Authorization': 'OSS '+accesskey+':'+sign
},
body:'{test:test}'
};
request.put(options, function(error, response, body) {
console.info('response:' + JSON.stringify(response));
console.info("statusCode:" + response.statusCode)
console.info('body: ' + body );
});
Documentation
For more information about how to use the OSS SDK, see OSS add signatures to headers.
Scope
- OSS