Por padrão, se você fizer upload de um objeto com o mesmo nome de um objeto existente, o objeto existente será substituído. Este tópico descreve como configure o cabeçalho de solicitação x-oss-forbid-overwrite para impedir que objetos existentes sejam substituídos por outros com nomes idênticos durante operações de cópia, upload simples ou multipart upload.
Upload simples
O exemplo de código a seguir mostra como impedir que objetos existentes sejam substituídos por novos com o mesmo nome durante um upload simples:
const OSS = require("ali-oss");
const client = new OSS({
// Set region 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 running the 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'
});
// Set yourLocalFile to the full path of the local file.
const file = "yourLocalFile";
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
"x-oss-forbid-overwrite": true,
};
async function put() {
try {
// Set yourObjectName to the full path of the object.
const result = await client.put("yourObjectName", file, { headers });
console.log(result);
} catch (e) {
console.log(e);
}
}
put();
Copiar um objeto
Copiar um objeto pequeno
Use o código a seguir para copiar um objeto pequeno sem substituir um objeto existente com o mesmo nome:
const OSS = require("ali-oss");
const client = new OSS({
// Set region 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 running the 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'
});
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
"x-oss-forbid-overwrite": true,
};
// Set yourTargetObject to the full path of the destination object.
// Set yourSourceObject to the full path of the source object.
client
.copy("yourTargetObject", "yourSourceObject", { headers })
.then((res) => {
console.log(res.res.data.toString("utf8"));
console.log(res);
})
.catch((e) => {
console.log(e);
});
Copiar um objeto grande
Para copiar um objeto grande via multipart copy e evitar a substituição de um objeto existente com o mesmo nome, use o seguinte exemplo:
const OSS = require("ali-oss");
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 running the 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,
// Set yourTargetBucket to the name of the bucket where the destination object is located.
bucket: "yourTargetBucket"
});
async function put() {
try {
const result = await client.multipartUploadCopy(
// Specify the full path of the destination object.
"yourTargetObject",
{
// Specify the full path of the source object to copy.
sourceKey: "yourSourceObject",
// Specify the name of the bucket where the source object is located.
sourceBucketName: "yourSourceBucket",
},
{
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
headers: { "x-oss-forbid-overwrite": true },
}
);
console.log(result);
} catch (e) {
console.log(e);
}
}
put();
Multipart upload
O exemplo a seguir mostra como impedir a substituição de um objeto existente ao fazer upload de um novo objeto com o mesmo nome usando multipart upload:
const OSS = require("ali-oss");
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 running the 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,
// Set yourBucketName to the name of the bucket.
bucket: "yourBucketName",
});
// Set yourLocalFile to the full path of the local file.
const file = "yourLocalFile";
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
"x-oss-forbid-overwrite": true,
};
{
// Perform a multipart upload. Set yourObjectName to the full path of the object.
client
.multipartUpload("yourObjectName", file, { headers })
.then((res) => {
console.log(res);
})
.catch((e) => {
console.log(e);
});
}
Referências
Para obter mais informações sobre a operação de API de upload simples, consulte PutObject.
Para obter detalhes sobre a operação de API de cópia de objetos, consulte CopyObject.
Para saber mais sobre as operações de API de multipart upload, consulte InitiateMultipartUpload e CompleteMultipartUpload.