Cette rubrique explique comment supprimer un ou plusieurs objets à l'aide du SDK OSS pour Harmony.
Notes d'utilisation
Pour connaître les régions et les endpoints pris en charge, consultez Régions et endpoints.
Vous devez disposer de l'autorisation
oss:DeleteObjectpour supprimer un objet. Pour plus d'informations, consultez la section Attacher une stratégie personnalisée à un utilisateur RAM.
Exemples de code
Supprimer un seul objet
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID obtained from STS.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token obtained from STS.
securityToken: 'yourSecurityToken',
// 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: 'oss-cn-hangzhou',
});
// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object that you want to delete.
const key = 'yourObjectName';
/**
* Delete the specified object.
* Use the deleteObject method.
*/
const deleteObject = async () => {
try {
// Use the deleteObject method to delete the object from the bucket.
const res = await client.deleteObject({
bucket, // Specify the name of the bucket.
key, // Specify the name of the object.
});
// Display the result of the deletion.
console.log(JSON.stringify(res));
} catch (err) {
// Capture exceptions.
if (err instanceof RequestError) {
// If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The EC.
} else {
// Display other unknown types of errors.
console.log('unknown error: ', err);
}
}
};
// Call the deleteObject function to delete the object.
deleteObject();
Supprimer plusieurs objets
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID obtained from STS.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token obtained from STS.
securityToken: 'yourSecurityToken',
// 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: 'oss-cn-hangzhou',
});
// Specify the name of the bucket.
const bucket = 'yourBucketName';
/**
* Delete multiple objects at a time.
* Use the deleteMultipleObjects method to delete multiple objects from the bucket.
*/
const deleteMultipleObjects = async () => {
try {
// Call the deleteMultipleObjects method to delete multiple objects.
const res = await client.deleteMultipleObjects({
bucket, // Specify the name of the bucket.
delete: {
// Specify the names of the objects to be deleted.
object: [
{ key: 'yourObjectName1' }, // The first object name.
{ key: 'yourObjectName2' }, // The second object name.
{ key: 'yourObjectName3' }, // The third object name.
],
},
});
// Display the result of the deletion.
console.log(JSON.stringify(res));
} catch (err) {
// Capture exceptions.
if (err instanceof RequestError) {
// If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The EC.
} else {
// Display other unknown types of errors.
console.log('unknown error: ', err);
}
}
};
// Call the deleteMultipleObjects function to perform the deletion.
deleteMultipleObjects();