使用node.js从Google云存储删除多个对象 [英] Delete multiple objects fron google cloud storage using node.js

查看:48
本文介绍了使用node.js从Google云存储删除多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Google云存储中删除多个对象.我一次删除了一个对象.

I need to delete multiple objects from google cloud storage. I have deleted one object at a time.

这是我的代码:

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");
  }
});

但是我需要同时删除多个对象.有可能吗?

But I need to delete multiple objects simultaneously. Is it possible or not?

推荐答案

我们确实有 bucket#deleteFiles ,它将为您处理请求的限制.您可以使用 prefix 选项通过命名约定来定位多个图像,例如:

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!
  }
});

这篇关于使用node.js从Google云存储删除多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆