Utilice la operación put para cargar un archivo local en OSS.
Permisos
De forma predeterminada, una cuenta de Alibaba Cloud tiene permisos completos. Los usuarios de RAM o los roles de RAM bajo una cuenta de Alibaba Cloud no tienen ningún permiso por defecto. La cuenta de Alibaba Cloud o el administrador de la cuenta debe otorgar los permisos de operación mediante RAM policies o Bucket Policy.
|
API |
Action |
Descripción |
|
PutObject |
|
Carga un objeto. |
|
|
Obligatorio si especifica etiquetas de objeto mediante el encabezado |
|
|
|
Obligatorio si el encabezado |
|
|
|
Código de ejemplo
A continuación se muestra cómo cargar el archivo local examplefile.txt como un objeto llamado exampleobject.txt en el bucket denominado 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();
Preguntas frecuentes
¿Cómo me aseguro de que la URL devuelta después de cargar un archivo sea una URL HTTPS en lugar de una URL HTTP?
Para garantizar que la URL devuelta después de cargar un archivo sea una URL HTTPS, agregue el elemento de configuración secure y establezca su valor en true. Para obtener más información, consulte Configuration items.
Referencias
Para consultar el código de ejemplo completo de una carga simple, véase Ejemplos en GitHub.
Para obtener más información sobre la operación de API para una carga simple, consulte PutObject.