Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Upload a local file (Node.js SDK)

Son Güncelleme:Oct 01, 2026

Yerel bir dosyayı OSS'e yüklemek için Put işlemini kullanın.

İzinler

Varsayılan olarak bir Alibaba Cloud hesabı tam izinlere sahiptir. Bir Alibaba Cloud hesabına bağlı RAM kullanıcıları veya RAM rolleri varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı veya hesap yöneticisi, işlem izinlerini RAM ilkesi veya Bucket Policy aracılığıyla vermelidir.

API

Eylem

Açıklama

PutObject

oss:PutObject

Bir nesne yükler.

oss:PutObjectTagging

Bir nesne yüklerken x-oss-tagging üst bilgisini kullanarak nesne etiketlerini belirtmeniz gerekiyorsa gereklidir.

kms:GenerateDataKey

Bir nesne yüklerken X-Oss-Server-Side-Encryption: KMS üst bilgisi KMS olarak ayarlanmışsa gereklidir.

kms:Decrypt

Örnek kod

Aşağıdaki kod, examplefile.txt adlı yerel dosyanın examplebucket adlı pakete exampleobject.txt adlı bir nesne olarak nasıl yükleneceğini gösterir.

const OSS = require('ali-oss')
const path=require("path")

const client = new OSS({
  // Set yourregion 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 you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket',
});

// Custom request headers
const headers = {
  // Specify the storage class of the object.
  'x-oss-storage-class': 'Standard',
  // Specify the access permissions of the object.
  'x-oss-object-acl': 'private',
  // When the file is accessed using its URL, specify that the file is downloaded as an attachment. The downloaded file is named example.txt.
  'Content-Disposition': 'attachment; filename="example.txt"',
  // Set tags for the object. You can set multiple tags at the same time.
  'x-oss-tagging': 'Tag1=1&Tag2=2',
  // Specify whether to overwrite an object that has the same name during a PutObject operation. This parameter is set to true to prevent the object from being overwritten.
  'x-oss-forbid-overwrite': 'true',
};

async function put () {
  try {
    // Specify the full path of the OSS object and the full path of the local file. The full path of the OSS object cannot contain the bucket name.
    // If you do not specify a local path in the full path of the local file, the file is uploaded from the local path of the project to which the sample program belongs.
    const result = await client.put('exampleobject.txt', path.normalize('D:\\localpath\\examplefile.txt')
    // Custom headers
    ,{headers}
    );
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();

SSS

Dosya yüklemesinden sonra döndürülen URL'nin HTTP yerine HTTPS URL olduğundan nasıl emin olabilirim?

Dosya yüklemesinden sonra döndürülen URL'nin bir HTTPS URL olduğundan emin olmak için secure yapılandırma öğesini ekleyin ve değerini true olarak ayarlayın. Daha fazla bilgi için bkz. Yapılandırma öğeleri.

İlgili Konular

  • Basit yükleme için eksiksiz örnek kodu görmek üzere GitHub örnekleri sayfasına bakın.

  • Basit yükleme API işlemi hakkında daha fazla bilgi için bkz. PutObject.