Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Download objects as files using OSS SDK for Node.js

Son Güncelleme:Oct 01, 2026

Bu konuda, bir paketten bilgisayarınıza nasıl nesne indireceğiniz açıklanmaktadır.

Örnek kod

Aşağıdaki örnek kod, examplebucket adlı bir paketteki exampleobject.txt adlı nesnenin yerel diskinizdeki D:\localpath yoluna nasıl indirileceğini gösterir:

const OSS = require('ali-oss');

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. 
  bucket: 'examplebucket'
});

async function get () {
  try {
    // Specify the full paths of the object and the local file. Do not include the bucket name in the full path of the object. 
    // If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved as a local file. 
    // If you do not specify a path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs. 
    const result = await client.get('exampleobject.txt', 'D:\\localpath\\examplefile.txt');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

get(); 

İlgili bilgiler

  • Nesneleri bilgisayarınıza indirmek için kullanılan eksiksiz örnek koda erişmek üzere GitHub adresini ziyaret edin.

  • Bir nesneyi yerel dosya olarak indirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetObject.