How to Use Node.js to Upload Files to Alibaba Cloud Object Storage

Requirements

How to Add Access Keys to the Credentials File

npm install ali-oss
npm install co

Initializing the Client

var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});

Installing Dependencies

mkdir sites/OSS-node-app && cd sites/OSS-node-app
{
"name": "OSS-node-app",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"license": "MIT"
}
npm install ali-oss express

Creating the Front-end App

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OSS Node Tutorial</title> <link rel="stylesheet" href="./style.css">
</head>
<body> <!—your content here --></body></html>
<h1>Something went wrong!</h1>
<p>Uploading the file was unsuccessful.</p>
<h1>Success!</h1>
<p>File uploaded successfully.</p>
<h1>OSS Tutorial</h1><p>Please select a file and submit form to upload.</p><form method="post" enctype="multipart/form-data" action="/upload">
<label for="file">Upload a file</label>
<input type="file" name="upload">
<input type="submit" class="button">
</form>
html {
font-family: sans-serif;
line-height: 1.5;
color: #333;
}
body {
margin: 0 auto;
max-width: 500px;
}
label,
input {
display: block;
margin: 5px 0;
}

Setting Up Server with Express

server.js
// Load dependencies
const ali = require(ali-oss');
const express = require('express');
const app = express();
// Views in public directory
app.use(express.static('public'));
// Main, error and success views
app.get('/', function (request, response) {
response.sendFile(__dirname + '/public/index.html');
});
app.get("/success", function (request, response) {
response.sendFile(__dirname + '/public/success.html');
});
app.get("/error", function (request, response) {
response.sendFile(__dirname + '/public/error.html');
});
server.js
app.listen(5000, function () {
console.log('Server listening on port 5000.');
});
npm start
Output
> node server.js
Server listening on port 5000.

Uploading our Files

server.js
const app = express();
// Set endpoint to Alibaba OSS
const spacesEndpoint = new OSS.Endpoint(' http://oss-cn-hangzhou.aliyuncs.com.');
const s3 = new OSS({
endpoint: spacesEndpoint
});
var co = require('co');
var OSS = require('ali-oss')
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>',
bucket: 'Your bucket name'
});
co(function* () {
var result = yield client.put('object-key', 'local-file');
console.log(result);
}).catch(function (err) {
console.log(err);
});
var co = require('co');
var OSS = require('ali-oss');
var fs = require('fs');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>',
bucket: 'Your bucket name'
});
co(function* () {
// use 'chunked encoding'
var stream = fs.createReadStream('local-file');
var result = yield client.putStream('object-key', stream);
console.log(result);
// do not use 'chunked encoding'
var stream = fs.createReadStream('local-file');
var size = fs.statSync('local-file').size;
var result = yield client.putStream(
'object-key', stream, {contentLength: size});
console.log(result);
}).catch(function (err) {
console.log(err);
});
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>',
bucket: 'Your bucket name'
});
co(function* () {
var result = yield client.put('object-key', new Buffer('hello world'));
console.log(result);
}).catch(function (err) {
console.log(err);
});
var co = require('co');
var OSS = require('ali-oss')
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>',
bucket: 'Your bucket name'
});
co(function* () {
var result = yield client.multipartUpload('object-key', 'local-file', {
progress: function* (p) {
console.log('Progress: ' + p);
}
meta: {
year: 2017,
people: 'test'
}
});
console.log(result);
var head = yield client.head('object-key');
console.log(head);
}).catch(function (err) {
console.log(err);
});
var progress = function (p) {
return function (done) {
console.log(p);
done();
};
};

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store