Todos los productos
Search
Centro de documentos

Object Storage Service:Upload objects using OSS SDK for Node.js

Última actualización: Sep 03, 2026

Este tema describe cómo cargar objetos en un bucket con control de versiones habilitado en Object Storage Service (OSS).

Carga simple

Si inicia una solicitud para cargar un objeto en un bucket con control de versiones habilitado, OSS genera un ID de versión único para el objeto cargado e incluye dicho ID en la respuesta como valor del encabezado x-oss-version-id. Si carga un objeto en un bucket con control de versiones suspendido, OSS genera un ID de versión null para el objeto. Si el objeto que carga tiene el mismo nombre que uno existente, el objeto existente se sobrescribe con el nuevo. De este modo, cada objeto conserva únicamente una versión cuyo ID es null.

A continuación se muestra cómo realizar una carga simple:

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: 'yourbucketname'
});

async function put() {
  const result = await client.put('fileName', path.normalize('D:\\localpath\\examplefile.txt'));
  console.log(result.res.headers['x-oss-version-id']); // Display the version ID of the uploaded object. 
}
put();

Carga por anexión

En un bucket con control de versiones habilitado, la operación AppendObject solo puede ejecutarse sobre la versión actual de un objeto anexable.

Nota
  • Al ejecutar la operación AppendObject sobre la versión actual de un objeto anexable, OSS no genera una versión anterior del objeto.

  • Si ejecuta la operación PutObject o DeleteObject sobre un objeto cuya versión actual es anexable, OSS almacena dicho objeto como una versión anterior y evita que se le sigan anexando datos.

  • No es posible ejecutar la operación AppendObject sobre un objeto cuya versión actual no sea anexable, como un objeto normal o un marcador de eliminación.

A continuación se muestra cómo realizar una carga por anexión:

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: 'yourbucketname'
});

async function append() {
  await client.append('filename', path.normalize('D:\\localpath\\examplefile.txt'), {
    partSize: 100 * 1024
  });
}
append();

Carga multiparte

Si llama al método MultipartUpload para completar una tarea de carga multiparte de un objeto en un bucket con control de versiones habilitado, OSS genera un ID de versión único para el objeto y lo devuelve en el campo x-oss-version-id del encabezado de respuesta.

A continuación se muestra cómo realizar una carga multiparte:

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: 'yourbucketname'
});

async function multipartUpload() {
  const result = await client.multipartUpload('filename', path.normalize('D:\\localpath\\examplefile.txt'), {
    partSize: 100 * 1024
  });
  // Display the version ID of the uploaded object. 
  console.log(result.res.headers['x-oss-version-id']); 
}
multipartUpload();

Referencias

  • Para obtener más información sobre la operación de API que permite realizar una carga simple, consulte PutObject.

  • Para obtener más información sobre la operación de API que permite realizar una carga por anexión, consulte AppendObject.

  • Para obtener más información sobre la operación de API que permite realizar una carga multiparte, consulte CompleteMultipartUpload.