Tek bir nesneyi, ada göre birden çok nesneyi, belirli bir önekle eşleşen nesneleri veya tüm bir dizini silebilirsiniz.
Silinen nesneler kurtarılamaz. Lütfen dikkatli olun.
Tek bir dosya silme
Aşağıdaki kod, examplebucket adlı paketten exampleobject.txt adlı bir nesneyi siler:
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this 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',
});
async function deleteObject() {
try {
// Specify the full path of the object. The full path cannot contain the bucket name.
const result = await client.delete('exampleobject.txt');
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteObject();
Birden çok dosyayı toplu olarak silme
Ada, öneke veya dizine göre aynı anda en fazla 1.000 nesneyi silebilirsiniz.
Toplu silme işlemine yönelik yanıt iki moddan birinde olabilir:
-
Ayrıntılı mod: OSS'den gelen yanıt gövdesi, silinen her nesne için sonuçları içerir. Bu, varsayılan moddur.
-
Sessiz mod: OSS bir yanıt gövdesi döndürmez.
Nesneleri otomatik olarak silmek için yaşam döngüsü kuralları da yapılandırabilirsiniz. Daha fazla bilgi için bkz. Son değiştirme zamanına dayalı yaşam döngüsü kuralları.
Belirtilen ada sahip birden çok nesneyi silme
Aşağıdaki kod, ada göre birden çok nesneyi siler:
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this 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',
});
async function deleteMulti() {
try {
// Specify the full paths of the objects to delete and set the response mode to verbose. The full paths cannot contain the bucket name.
// let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt']);
// console.log(result);
// Specify the full paths of the objects to delete and set the response mode to quiet. The full paths cannot contain the bucket name.
const result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt'], {quiet: true});
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteMulti();
Belirtilen nesne adı ön ekiyle veya dizin içinde birden çok nesneyi silme
Aşağıdaki kod, nesneleri öneke göre siler veya tüm bir dizini siler:
Önek belirtilmezse veya NULL olarak ayarlanırsa paketteki tüm nesneler silinir. Önekleri dikkatle belirtin.
const OSS = require('ali-oss');
const client = new OSS({
// Specify 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: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this 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',
});
// Handle failed requests to prevent promise.all from breaking, and return the failure reason and the failed file name.
async function handleDel(name) {
try {
await client.delete(name);
} catch (error) {
error.failObjectName = name;
return error;
}
}
// Delete multiple files.
async function deletePrefix(prefix) {
const list = await client.list({
prefix: prefix,
});
list.objects = list.objects || [];
const result = await Promise.all(list.objects.map((v) => handleDel(v.name)));
console.log(result);
}
// To delete all objects that have the 'src' prefix, set prefix to 'src'. This deletes files such as 'src_file.txt', the 'src' folder, and all files within the 'src' folder.
deletePrefix('src');
// To delete only the 'src' folder and all files within it, set prefix to 'src/'.
// deletePrefix('src/');
İlgili konular
-
Bir veya daha fazla dosyanın nasıl silineceğini gösteren eksiksiz örnek kod için GitHub örneğine bakın.
-
Tek bir dosya silmeye yönelik API referansı için bkz. DeleteObject.
-
Birden çok dosya silmeye yönelik API referansı için bkz. DeleteMultipleObjects.