Use o upload via streaming para enviar fluxos de dados, como streams de arquivos ou de rede, para um objeto em um bucket.
Upload de stream de arquivo
Este exemplo envia uma stream de arquivo para um objeto chamado exampleobject.txt no diretório exampledir do bucket 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 {
// By default, the SDK uses chunked encoding and sends an HTTP PUT request when you call the putStream operation.
// Specify the full path of the local source file.
// If you omit the local path, the file is uploaded from the script's directory.
let 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.
let result = await client.putStream('exampledir/exampleobject.txt', stream);
// To disable chunked encoding, specify the contentLength parameter in the options.
// let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
// let size = fs.statSync('D:\\localpath\\examplefile.txt').size;
// let result = await client.putStream(
// The stream parameter can be any Readable Stream object, such as a file stream or a network stream.
// 'exampledir/exampleobject.txt', stream, {contentLength: size});
console.log(result);
} catch (e) {
console.log(e)
}
}
putStream();
Upload de stream de rede
Este exemplo envia uma stream de rede para um objeto chamado example.png no bucket examplebucket.
const OSS = require("ali-oss");
const fs = require("fs");
const urllib = require("urllib");
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',
});
// Specify the URL of the network stream.
const url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/en-US/20220908/cbgh/image_processing_example.jpg";
// Import the Duplex stream.
// The stream parameter can be any Readable Stream object, such as a file stream or a network stream.
const Duplex = require("stream").Duplex;
// Instantiate a Duplex stream.
let stream = new Duplex();
urllib.request(url, (err, data, res) => {
if (!err) {
// Push data into the Duplex stream.
stream.push(data);
stream.push(null);
client
// Specify the full path of the object, for example, 'example.png'. The full path cannot contain the bucket name.
.putStream("example.png", stream)
.then((r) => console.log(r))
.catch((e) => console.log(e));
}
});
Referências
Consulte o código de exemplo completo de upload via streaming no GitHub.