使用 node.js 從谷歌云存儲中刪除多個對象 (Delete multiple objects fron google cloud storage using node.js)


問題描述

使用 node.js 從谷歌云存儲中刪除多個對象 (Delete multiple objects fron google cloud storage using node.js)

我需要從谷歌云存儲中刪除多個對象。我一次刪除了一個對象。

這是我的代碼:

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

但是我需要同時刪除多個對象。有沒有可能?


參考解法

方法 1:

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

方法 2:

There is no way to atomically delete multiple objects from GCS at the same time.

However, you can issue multiple requests with a single call, which returns the status of each individual operation, using the batch API.

方法 3:

I created an array of all the objects that I wanted to delete and then iterated over the array, executing the "delete" function specified on the google cloud storage documentation.

Delete GCS object

const storage = new Storage({keyFilename: 'google‑credentials.json'});
const imagesToDelete = ['fileName1', 'fileName2', 'fileName3'];

    imagesToDelete.map(async (image) => {
      await storage
        .bucket('yourbucketName')
        .file(image)
        .delete();
    });

(by Abdul ManafStephenjterraceKauã Steliano)

參考文件

  1. Delete multiple objects fron google cloud storage using node.js (CC BY‑SA 2.5/3.0/4.0)

#npm #gcloud-node #node.js #google-cloud-storage






相關問題

TFS 構建中的 npm install 拋出“未找到:git” (npm install in TFS build throws "not found: git")

找不到模塊“電子” (Cannot find module 'electron')

使用 npm 進行生成器角度安裝時出現問題 (Trouble during generator-angular installation with npm)

使用 node.js 從谷歌云存儲中刪除多個對象 (Delete multiple objects fron google cloud storage using node.js)

VS Task Runner Explorer - Node Sass 找不到綁定 (VS Task Runner Explorer - Node Sass could not find a binding)

有什麼東西可以跟踪我安裝的 npm 包並在不同的開發環境中恢復它們嗎?(如“恢復 NuGet 包”) (Is there anything that can keep track of my installed npm packages and restore them on a different dev environment ? (like "Restore NuGet packages"))

角度材質工具欄不繼承自角度材質 (Angular material toolbar not inheriting from angular materials)

npm install throws 和錯誤告訴 node 不是一個可識別的命令 (npm install throws and error telling node is not a recognized command)

從代理後面登錄的無服務器框架? (Serverless Framework Login From Behind a Proxy?)

構建工具結構 - NPM、Grunt 和 Wordpress (Build tools structure - NPM, Grunt with Wordpress)

如何解決 Linux 中的“npm ERR!code ELIFECYCLE”錯誤? (How to solve "npm ERR! code ELIFECYCLE" error in Linux?)

如何在 Angular 中導入“數字”:“^1.2.6”? (How to import "numeric": "^1.2.6" in Angular?)







留言討論