Par défaut, si vous téléchargez un objet portant le même nom qu'un objet existant, ce dernier est écrasé. Cette rubrique explique comment configurer l'en-tête de requête x-oss-forbid-overwrite pour empêcher l'écrasement des objets existants lors de la copie d'objets ou de l'exécution d'un téléchargement simple ou multipartie.
Téléchargement simple
L'exemple de code suivant montre comment empêcher l'écrasement des objets existants portant le même nom lors d'un téléchargement simple :
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();
Copier un objet
Copier un petit objet
L'exemple de code suivant montre comment copier un petit objet sans écraser l'objet existant portant le même nom :
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);
});
Copier un grand objet
L'exemple de code suivant montre comment empêcher l'écrasement d'un objet existant par un grand objet portant le même nom lors de la copie de ce dernier à l'aide d'une copie multipartie :
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();
Téléchargement multipartie
L'exemple de code suivant montre comment empêcher l'écrasement d'un objet existant par un objet portant le même nom lors du téléchargement de cet objet via un téléchargement multipartie :
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);
});
}
Références
Pour plus d'informations sur l'opération API de téléchargement simple, consultez PutObject.
Pour plus d'informations sur l'opération API de copie d'objet, consultez CopyObject.
Pour plus d'informations sur les opérations API de téléchargement multipartie, consultez InitiateMultipartUpload et CompleteMultipartUpload.