Todos os produtos
Search
Central de documentação

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

Última atualização: Aug 20, 2026

Use a operação put para fazer upload de um arquivo local no OSS.

Permissões

Por padrão, uma conta Alibaba Cloud tem permissões totais. Usuários RAM ou funções RAM vinculados a essa conta não têm permissões iniciais. A conta Alibaba Cloud ou o administrador da conta deve conceder as permissões operacionais necessárias por meio de RAM policies ou Bucket Policy.

API

Action

Description

PutObject

oss:PutObject

Faz upload de um objeto.

oss:PutObjectTagging

Necessário ao especificar tags de objeto com o cabeçalho x-oss-tagging durante o upload.

kms:GenerateDataKey

Obrigatório se o cabeçalho X-Oss-Server-Side-Encryption: KMS estiver definido como KMS durante o upload do objeto.

kms:Decrypt

Código de exemplo

O código a seguir mostra como fazer upload do arquivo local examplefile.txt como um objeto chamado exampleobject.txt no bucket 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();

Perguntas frequentes

Como garantir que a URL retornada após o upload seja HTTPS em vez de HTTP?

Para garantir que a URL retornada após o upload use o protocolo HTTPS, adicione o item de configuração secure e defina seu valor como true. Para mais informações, consulte Configuration items.

Referências

  • Para obter o código de exemplo completo de upload simples, consulte GitHub examples.

  • Para obter mais informações sobre a operação de API de upload simples, consulte PutObject.