Delete folder in Google Cloud Storage using nodejs gcloud api

流过昼夜 提交于 2019-12-29 08:13:19

问题


I am using gcloud nodejs api to access Google Cloud Storage. I can save/delete/exists files individually, but I didn't find a way to delete a folder or even to list the files in a folder using gcloud nodejs api.

I have seen people say that the folder hierachy in GCS is not a real tree structure, but just names. So I tried to use wildcard to match the file name string, which did not succeed.

I wonder if there is any way to do it. If not, what tool should I use?


回答1:


The code to list files in a directory should look something like this:

bucket.getFiles({ prefix: 'directoryName/' }, function(err, files) {})

And to delete:

bucket.deleteFiles({ prefix: 'directoryName/' }, function(err) {})
  • getFiles API documentation
  • deleteFiles API documentation



回答2:


Instead of using gcloud nodejs api, there are two other ways to do this.

  1. Use the googleapis package to access the standard JSON API and XML API of gcs. googleapis is a lower level API tool which includes interacting with google cloud services. We can create/list/delete files on gcs. Documentation and examples:

    • https://cloud.google.com/storage/docs/json_api/v1/objects/delete
    • https://cloud.google.com/storage/docs/json_api/v1/objects/list
  2. Use childe_process to execute the gsutil commmand line tool. This is not a standard way of programatically accessing the google api, but still a viable solution.Wildcard is allowed when issuing the command. Note that is may not work on the google app engine. Here is an example.

Nodejs

var exec = require('child_process').exec;
exec("gsutil rm gs://[bucketname]/[directory ]/*" , function(error,stdout,stderr){});

As Stephen suggested, using standard gcloud method bucket.getFiles and bucket.deleteFiles is the most desirable approach. Since gcs don't have the concept of directories, the manipulation of multiple files obviously should be considered as a bucket level operation.



来源:https://stackoverflow.com/questions/38597802/delete-folder-in-google-cloud-storage-using-nodejs-gcloud-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!