Tous les produits
Search
Centre de documentation

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

Dernière mise à jour :Aug 21, 2026

Utilisez l'opération put pour téléverser un fichier local vers OSS.

Autorisations

Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune autorisation par défaut. Le compte Alibaba Cloud ou l'administrateur du compte doit accorder les autorisations d'exploitation via des politiques RAM ou une Bucket Policy.

API

Action

Description

PutObject

oss:PutObject

Téléverse un objet.

oss:PutObjectTagging

Requis si vous spécifiez des tags d'objet à l'aide de l'en-tête x-oss-tagging lors du téléversement d'un objet.

kms:GenerateDataKey

Requis si l'en-tête X-Oss-Server-Side-Encryption: KMS est défini sur KMS lors du téléversement d'un objet.

kms:Decrypt

Exemple de code

Le code suivant montre comment téléverser le fichier local examplefile.txt en tant qu'objet nommé exampleobject.txt dans le bucket nommé examplebucket.

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

FAQ

Comment m'assurer que l'URL renvoyée après le téléversement d'un fichier est une URL HTTPS et non une URL HTTP ?

Pour garantir que l'URL renvoyée après le téléversement d'un fichier soit une URL HTTPS, ajoutez l'élément de configuration secure et définissez sa valeur sur true. Pour plus d'informations, consultez les éléments de configuration.

Références

  • Pour obtenir l'exemple de code complet pour un téléversement simple, consultez les exemples GitHub.

  • Pour plus d'informations sur l'opération API relative au téléversement simple, consultez l'opération PutObject.