Use the putStream method to upload any readable stream — a local file stream, a network stream, or any other readable data source — directly to an OSS object.
By default, the OSS Node.js SDK uses chunked encoding and sends an HTTP PUT request. To disable chunked encoding, pass the contentLength option with the exact byte size of the stream.
Prerequisites
Before you begin, ensure that you have:
Installed the
ali-osspackage (npm install ali-oss)An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid credentials
putStream parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Full path of the target object (e.g., exampledir/exampleobject.txt). Do not include the bucket name. |
stream | Readable stream | Yes | Any readable stream object, such as a file stream or a network stream. |
options.contentLength | number | No | Byte size of the stream. When set, disables chunked encoding. For local files, get this value with fs.statSync(path).size. |
The method returns a result object with the following key fields:
| Field | Type | Description |
|---|---|---|
res.status | number | HTTP status code of the upload response |
res.headers | object | Response headers |
Upload a file stream
The following example uploads a local file to exampledir/exampleobject.txt in examplebucket.
const OSS = require('ali-oss');
const fs = require('fs');
const client = new OSS({
// Specify the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name, for example, examplebucket.
bucket: 'examplebucket',
});
async function putStream() {
try {
// Specify the full path of the local source file.
// If you omit the local path, the file is uploaded from the script's directory.
const stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
// Specify the full path of the object, for example, 'exampledir/exampleobject.txt'. The full path cannot contain the bucket name.
const result = await client.putStream('exampledir/exampleobject.txt', stream);
console.log(result);
} catch (e) {
console.log(e);
}
}
putStream();To disable chunked encoding, pass contentLength in the options:
const stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
const size = fs.statSync('D:\\localpath\\examplefile.txt').size;
const result = await client.putStream('exampledir/exampleobject.txt', stream, { contentLength: size });
console.log(result);Upload a network stream
The following example fetches an image from a URL and uploads it as example.png in examplebucket. It wraps the HTTP response body in a Duplex stream before passing it to putStream.
const OSS = require('ali-oss');
const urllib = require('urllib');
const { Duplex } = require('stream');
const client = new OSS({
// Specify the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name, for example, examplebucket.
bucket: 'examplebucket',
});
const url = 'https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/en-US/20220908/cbgh/image_processing_example.jpg';
urllib.request(url, (err, data) => {
if (err) {
console.log(err);
return;
}
// Wrap the response data in a Duplex stream.
const stream = new Duplex();
stream.push(data);
stream.push(null);
// Specify the full path of the object, for example, 'example.png'. The full path cannot contain the bucket name.
client
.putStream('example.png', stream)
.then((result) => console.log(result))
.catch((e) => console.log(e));
});References
For the complete sample code, see putStream on GitHub.