const { Storage } = require ( '@google-cloud/storage' ) ; const sleep = async ( time ) => { return new Promise ( ( resolve ) => setTimeout ( resolve , time ) ) ; } ; export const createClient = ( credentials ? ) => { return new Storage ( { credentials } ) ; } ; export const createBucket = async ( client , bucketName , location = 'US-CENTRAL1' , storageClass = 'COLDLINE' ) => { await client . createBucket ( bucketName , { location , storageClass , } ) ; await sleep ( 2000 ) ; } ; export const createBucketIfNotExists = async ( client , bucketName , location = 'US-CENTRAL1' , storageClass = 'COLDLINE' ) => { if ( ! ( await checkBucketExists ( client , bucketName ) ) ) { await createBucket ( client , bucketName , location , storageClass ) ; } } ; export const getBuckets = async ( client ) => { const [ buckets ] = await client . getBuckets ( ) ; return buckets ; } ; export const getFiles = async ( client , bucketName ) => { const [ files ] = await client . bucket ( bucketName ) . getFiles ( ) ; return files ; } ; export const checkBucketExists = async ( client , bucketName ) => { const buckets = await getBuckets ( client ) ; return buckets . some ( ( bucket ) => bucket . name === bucketName ) ; } ; export const deleteBucket = async ( client , bucketName ) => { await client . bucket ( bucketName ) . delete ( ) ; } ; export const fileUpload = async ( client , bucketName , srcFileName , useCache = true ) => { const uploadOptions = { gzip : true , metadata : { cacheControl : useCache ? 'public, max-age=31536000' : 'no-cache' , } , } ; await client . bucket ( bucketName ) . upload ( srcFileName , uploadOptions ) ; } ; export const fileDownload = async ( client , bucketName , srcFileName , dstFileName ) => { await client . bucket ( bucketName ) . file ( srcFileName ) . download ( { destination : dstFileName , } ) ; } ;