6

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?

Yuqing
  • 333
  • 3
  • 13

2 Answers2

23

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) {})
Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48
Stephen
  • 5,710
  • 1
  • 24
  • 32
  • 1
    The links are out of date. Anyway, I couldn't find any reference to the `deleteFiles` function in the docs. I had to look at the source code. Just one addition: without the option `delimiter: '/'` the function deletes all files and subfolders. – Samuel Méndez Jan 03 '20 at 12:00
  • 1
    @SamuelMéndez just updated the links in @Stephen's answer. `deleteFiles` does exist thankfully, documentation is [here](https://googleapis.dev/nodejs/storage/latest/Bucket.html#deleteFiles). Cheers – Greg Sadetsky Dec 26 '20 at 17:37
  • https://cloud.google.com/storage/docs/samples/storage-delete-file#storage_delete_file-nodejs – howMuchCheeseIsTooMuchCheese Dec 17 '21 at 18:38
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:

  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.

Yuqing
  • 333
  • 3
  • 13
  • I tried the solution using gsutil within my Firebase Cloud Function and I get following error: gsutil: not found Is there a way to install gsutil on the Google Service ? – Thomas Jul 13 '17 at 16:05
  • thinking of trying this in a cloud run instance. do you know if the images pre-install gcloud on them? – maxwell Jul 19 '20 at 19:08