Use OSS SDK for HarmonyOS to delete a single object or multiple objects from a bucket.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
oss:DeleteObjectpermission on the target objects. For setup instructions, see Attach a custom policy to a RAM userThe region where your bucket is located. For a full list of regions and endpoints, see Regions and endpoints
Delete a single object
Call deleteObject to remove one object from a bucket.
import Client, { RequestError } from '@aliyun/oss';
const client = new Client({
accessKeyId: '<your-access-key-id>', // AccessKey ID obtained from STS
accessKeySecret: '<your-access-key-secret>', // AccessKey secret obtained from STS
securityToken: '<your-sts-token>', // STS token obtained from STS
region: 'oss-cn-hangzhou', // Region where the bucket is located
});
const deleteObject = async () => {
try {
const res = await client.deleteObject({
bucket: '<your-bucket-name>',
key: '<your-object-key>',
});
console.log(JSON.stringify(res));
} catch (err) {
if (err instanceof RequestError) {
console.log('code: ', err.code);
console.log('message: ', err.message);
console.log('requestId: ', err.requestId);
console.log('status: ', err.status);
console.log('ec: ', err.ec);
} else {
console.log('unknown error: ', err);
}
}
};
deleteObject();Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | String | Yes | Name of the bucket that contains the object |
key | String | Yes | Full key (path) of the object to delete |
Replace the following placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | AccessKey ID obtained from Security Token Service (STS) | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret obtained from STS | xXxXxXx |
<your-sts-token> | STS token obtained from STS | CAISxxx |
<your-bucket-name> | Name of the bucket | my-bucket |
<your-object-key> | Full key of the object to delete | images/photo.jpg |
Delete multiple objects
Call deleteMultipleObjects to remove multiple objects in a single request.
import Client, { RequestError } from '@aliyun/oss';
const client = new Client({
accessKeyId: '<your-access-key-id>',
accessKeySecret: '<your-access-key-secret>',
securityToken: '<your-sts-token>',
region: 'oss-cn-hangzhou',
});
const deleteMultipleObjects = async () => {
try {
const res = await client.deleteMultipleObjects({
bucket: '<your-bucket-name>',
delete: {
object: [
{ key: '<your-object-key-1>' },
{ key: '<your-object-key-2>' },
{ key: '<your-object-key-3>' },
],
},
});
console.log(JSON.stringify(res));
} catch (err) {
if (err instanceof RequestError) {
console.log('code: ', err.code);
console.log('message: ', err.message);
console.log('requestId: ', err.requestId);
console.log('status: ', err.status);
console.log('ec: ', err.ec);
} else {
console.log('unknown error: ', err);
}
}
};
deleteMultipleObjects();Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | String | Yes | Name of the bucket that contains the objects |
delete.object | Array | Yes | List of objects to delete. Each entry must include a key field |
Replace the following placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Name of the bucket | my-bucket |
<your-object-key-1> ... <your-object-key-3> | Full key of each object to delete | logs/2024-01-01.log |