Last active
April 21, 2020 10:50
-
-
Save vijayakumar-psg587/bc972009070399fe7daa7cfb42db4013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function uploadEnvFiles(env_name: string) { | |
const LOGGER: pino.Logger = PinoLoggerServiceInstance.getLogger(__filename); | |
return new Promise(async (res, rej) => { | |
// Get the storage bucket | |
const str = GcloudAuthenticationInstance.createGcloudAuthenticationBucket(); | |
// get the bucket name | |
const bucketToUpload = GCLOUD_ENV_STR_BUCKET_NAME; | |
let uploadLocalFilePath; | |
let destinationBucketPath; | |
// Custom implementation - determine the file to be uploaded based on the env | |
if (!AppUtilServiceInstance.isNullOrUndefined(env_name)) { | |
uploadLocalFilePath = | |
ENV_NAME_DEV === env_name | |
? GCLOUD_UPLOAD_FILE_DEV_LOCAL_PATH | |
: GCLOUD_UPLOAD_FILE_PROD_LOCAL_PATH; | |
destinationBucketPath = | |
ENV_NAME_DEV === env_name ? GCLOUD_DATABASE_BUCKET_DEV : GCLOUD_DATABASE_BUCKET_PROD; | |
} | |
LOGGER.info('after authentication'); | |
// Use pump module to do the streaming | |
pump(fs.createReadStream(uploadLocalFilePath),str.bucket(bucketToUpload) | |
.file(destinationBucketPath) | |
.createWriteStream({ | |
gzip: true, | |
public: true, | |
resumable: true, | |
}) | |
).on('error', (err) => { | |
LOGGER.error('Error occured in uploading:', err); | |
rej({ status: 'Error', error: err, code: 500 }); | |
}).on('finish', () => { | |
LOGGER.info('Successfully uploaded the file'); | |
res({ status: 'Success', code: 201, error: null }); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment