Tüm Ürünler
Search
Belge Merkezi

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

Son Güncelleme:Oct 01, 2026

Akış yüklemeyi kullanarak dosya veya ağ akışları gibi veri akışlarını bir paket içindeki bir nesneye yükleyin.

Dosya akışı yükleme

Bu örnekte, bir dosya akışı examplebucket paketinin exampledir dizinindeki exampleobject.txt adlı bir nesneye yüklenir.

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();

Ağ akışı yükleme

Bu örnekte, bir ağ akışı examplebucket paketindeki example.png adlı bir nesneye yüklenir.

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));
  }
});

İlgili Konular

Akış yükleme için tam örnek kodu GitHub adresinde bulabilirsiniz.