All files / libs/vendors/google-cloud/cloud-storage/src/lib cloud-storage-count.service.ts

100% Statements 22/22
66.66% Branches 2/3
100% Functions 2/2
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { Storage } from '@google-cloud/storage';
import { Injectable } from '@nestjs/common';
 
@Injectable()
export class CloudStorageCountService {
  public constructor(private readonly storage: Storage) {}
 
  /**
   * Get the number of files in a bucket
   * @param bucket The bucket name
   * @param delimiter The delimiter to use (used to exclude some files).
   * > Results will contain only objects whose names, aside from the prefix, do not contain delimiter.
   * > Objects whose names, aside from the prefix, contain delimiter will have their name truncated after the delimiter,
   * > returned in apiResponse.prefixes.
   * > Duplicate prefixes are omitted.
   * @see https://googleapis.dev/nodejs/storage/latest/Bucket.html#getFiles
   */
  public async countBucketFiles(bucket: string, delimiter?: string): Promise<number> {
    const [files] = await this.storage.bucket(bucket).getFiles({ delimiter });
    return files.length;
  }
}