Tous les produits
Search
Centre de documentation

Object Storage Service:Streaming Upload (Node.js SDK)

Dernière mise à jour :Aug 18, 2026

Le chargement en flux continu permet de transférer des flux de données, tels que des flux de fichiers ou réseau, vers un objet d'un bucket.

Charger un flux de fichier

Cet exemple charge un flux de fichier vers un objet nommé exampleobject.txt dans le répertoire exampledir du 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();

Charger un flux réseau

Cet exemple charge un flux réseau vers un objet nommé example.png dans le 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));
  }
});

Références

Consultez l'exemple de code complet pour le chargement en flux continu sur GitHub.