Skip to content

Instantly share code, notes, and snippets.

@vijayakumar-psg587
Last active April 21, 2020 10:50
Show Gist options
  • Save vijayakumar-psg587/bc972009070399fe7daa7cfb42db4013 to your computer and use it in GitHub Desktop.
Save vijayakumar-psg587/bc972009070399fe7daa7cfb42db4013 to your computer and use it in GitHub Desktop.
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