我需要从Google云端存储中删除多个对象。我一次删除了一个对象。
这是我的代码:
var gcloud = require('gcloud')({
projectId: "sampleProject1"
});
var gcs = gcloud.storage();
var myBucket = gcs.bucket('sampleBucket1');
var file = myBucket.file('1.png');
file.delete(function (err, apiResponse) {
if (err) {
console.log(err);
}
else {
console.log("Deleted successfully");
}
});
但我需要同时删除多个对象。有可能吗?
答案 0 :(得分:4)
We do have bucket#deleteFiles
that will handle throttling the requests for you. You can use the prefix
option to target multiple images by a naming convention, like:
bucket.deleteFiles({ prefix: 'image-' }, callback);
If that doesn't work, we also have a guide that shows how you can do the throttling logic yourself. See "My requests are returning errors instructing me to retry the request": https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/guides/troubleshooting
Edit to elaborate on how to do the throttling using async:
var async = require('async');
var PARALLEL_LIMIT = 10;
function deleteFile(file, callback) {
file.delete(callback);
}
async.eachLimit(filesToDelete, PARALLEL_LIMIT, deleteFile, function(err) {
if (!err) {
// Files deleted!
}
});
答案 1 :(得分:0)
无法同时从GCS原子删除多个对象。
但是,您可以通过一次调用发出多个请求,使用batch API返回每个操作的状态。
答案 2 :(得分:0)
我创建了一个要删除的所有对象的数组,然后在该数组上进行迭代,执行在Google云存储文档中指定的“删除”功能。
const storage = new Storage({keyFilename: 'google-credentials.json'});
const imagesToDelete = ['fileName1', 'fileName2', 'fileName3'];
imagesToDelete.map(async (image) => {
await storage
.bucket('yourbucketName')
.file(image)
.delete();
});
答案 3 :(得分:-3)
var gcloud = require('gcloud')({
projectId: "sampleProject1"
});
var gcs = gcloud.storage();
var myBucket = gcs.bucket('sampleBucket1');
var collection = gcs.collection("Add file for delete");
collection.insert({'1.png'},{'2.png'});
collection.delete(function (err, apiResponse) {
if (err) {
console.log(err);
}
else {
console.log("Deleted successfully");
}
});