All Products
Search
Document Center

Object Storage Service:Unggah yang dapat dilanjutkan (Node.js SDK)

Last Updated:Nov 30, 2025

Unggah yang dapat dilanjutkan membagi file menjadi beberapa bagian. Bagian-bagian tersebut diunggah secara terpisah, lalu digabungkan untuk membentuk file lengkap.

Catatan

Sintaksis catch pada contoh kode berikut didasarkan pada Promise ES6 dan async/await. Untuk informasi selengkapnya tentang cara menggunakan kit pengembangan perangkat lunak (SDK), lihat Install the Node.js SDK.

Untuk informasi selengkapnya tentang unggah yang dapat dilanjutkan, lihat Resumable upload. Anda juga dapat mengatur aturan lifecycle untuk menghapus fragmen yang tidak diinginkan secara berkala. Untuk informasi selengkapnya, lihat Delete fragments.

Fitur multipart upload menyediakan parameter progress yang dapat digunakan untuk menentukan callback progres. Dalam callback tersebut, SDK meneruskan persentase penyelesaian dan informasi breakpoint sebagai parameter. Untuk melakukan unggah yang dapat dilanjutkan, simpan informasi checkpoint selama proses unggah. Jika terjadi error, teruskan checkpoint yang telah disimpan ke fungsi `multipartUpload`. Unggah kemudian akan dilanjutkan dari titik kegagalan sebelumnya.

Kode contoh

Kode berikut menunjukkan cara melakukan unggah yang dapat dilanjutkan.

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample 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.
  bucket: 'examplebucket',
});

// Set yourfilepath to the local path of the file to upload.
const filePath = "yourfilepath";

let checkpoint;
async function resumeUpload() {
  // Retry five times.
  for (let i = 0; i < 5; i++) {
    try {
      const result = await client.multipartUpload('object-name', filePath, {
        checkpoint,
        async progress(percentage, cpt) {
          checkpoint = cpt;
        },
      });
      console.log(result);
      break; // Break the current loop.
    } catch (e) {
      console.log(e);
    }
  }
}

resumeUpload();

Contoh kode di atas menyimpan checkpoint ke dalam variabel. Jika program crash, informasi checkpoint akan hilang. Untuk mencegah hal ini, simpan checkpoint ke dalam file sehingga program dapat membaca kembali informasi tersebut setelah restart.

Referensi

Untuk kode contoh lengkap mengenai unggah yang dapat dilanjutkan, lihat contoh di GitHub.