すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:ストリーミングアップロード

最終更新日:Nov 07, 2024

このトピックでは、ファイルストリームやネットワークストリームなどのデータストリームをObject Storage Service (OSS) バケット内のオブジェクトにアップロードする方法について説明します。

ファイルストリームのアップロード

次のサンプルコードは、examplebucketバケットのexampledirディレクトリにあるexampleobject.txtオブジェクトにファイルストリームをアップロードする方法の例を示しています。

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

次のサンプルコードは、examplebucketバケットのexampledirディレクトリにあるexampleobject.txtオブジェクトにネットワークストリームをアップロードする方法の例を示しています。

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

関連ドキュメント

ストリーミングアップロードの完全なサンプルコードについては、『GitHub』をご参照ください。