This topic describes how to upload data streams such as file streams and network streams to objects in Object Storage Service (OSS) buckets.
Upload a file stream
The following sample code provides an example on how to upload a file stream to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:
const OSS = require('ali-oss');
const fs = require('fs');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the bucket. Example: examplebucket.
bucket: 'examplebucket',
});
async function putStream () {
try {
// Use chunked encoding. When you call putStream, OSS SDK initiates an HTTP PUT request for chunked encoding.
// Specify the full path of the local file. Data stream is read from the local file.
// If the path of the local file is not specified, the local file is uploaded from the path of the project to which the sample program belongs.
let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path.
let result = await client.putStream('exampledir/exampleobject.txt', stream);
// Do not use chunked encoding. If contentLength is specified in options, chunked encoding is not used.
// let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
// let size = fs.statSync('D:\\localpath\\examplefile.txt').size;
// let result = await client.putStream(
// You can set stream to specify the types of Readable Stream, such as file stream and network stream.
// 'exampledir/exampleobject.txt', stream, {contentLength: size});
console.log(result);
} catch (e) {
console.log(e)
}
}
putStream();
Upload a network stream
The following sample code provides an example on how to upload a network stream to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:
const OSS = require("ali-oss");
const fs = require("fs");
const urllib = require("urllib");
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the bucket. Example: examplebucket.
bucket: 'examplebucket',
});
// Specify the network stream URL.
const url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20220908/cbgh/IMG_example.jpg";
// Import the duplex stream.
// You can set stream to specify the types of Readable Stream, such as file stream and network stream.
const Duplex = require("stream").Duplex;
// Instantiate the duplex stream.
let stream = new Duplex();
urllib.request(url, (err, data, res) => {
if (!err) {
// Receive data from the duplex stream.
stream.push(data);
stream.push(null);
client
// Specify the full path of the object. Example: example.png. Do not include the bucket name in the full path.
.putStream("example.png", stream)
.then((r) => console.log(r))
.catch((e) => console.log(e));
}
});
References
For the complete sample code for streaming upload, visit GitHub.