Kaisar
Kaisar

Reputation: 53

Getting all files and subdirectories inside a specific directory in Google cloud storage

I have some files and folders stored in gcs. I want to get all the files and folders given a specific directory.

I have managed to get all the files inside a directory by using delimiter and a prefix. But the response is excluding the subdirectories which i want to get also.

  const prefix = `users/${currentUserName}/`;
  const delimiter = "/";
  const options = {
    prefix: prefix
  };
  if (delimiter) {
    options.delimiter = delimiter;
  }

  const [files] = await gcStorage.bucket(bucketName).getFiles(options);

I am getting outputs like:

  1. users/kaisar/
  2. users/kaisar/Attendance.docx
  3. users/kaisar/SampleAudio_0.4mb (1).mp3

I want outputs like:

  1. users/kaisar/
  2. users/kaisar/Attendance.docx
  3. users/kaisar/SampleAudio_0.4mb (1).mp3
  4. users/kaisar/newFolder/
  5. users/kaisar/videoFolder/

Upvotes: 0

Views: 1258

Answers (1)

John Hanley
John Hanley

Reputation: 81336

The sub-directories are returned as CommonPrefixes within the response (apiResponse.prefixes)

var query = {
  delimiter: '/'
};
bucket.getFiles(query, function(err, files, nextQuery, apiResponse) {
  // files is array of files
  // apiResponse.prefixes is the same as CommonPrefixes
});

Refer to the documentation for an example: link

Upvotes: 1

Related Questions