import AWS from 'aws-sdk'; import { MyGlobal as app } from "../globals"; export const uploadBufferToAWSCdn = (buffer: any, shipmentKey: string,isInvoice: boolean = false, isZpl: boolean = false): Promise => { return new Promise(async (resolve, reject) => { let targetPath = 's-' + shipmentKey + '-' + 'shipment.pdf'; if(isInvoice){ targetPath = 's-' + shipmentKey + '-' + 'invoice.pdf'; } try { app.logger!.info({ identifier: 'order-page-testing', shipmentKey: shipmentKey, targetPath: targetPath, where: 'ShipmentDocWorker.uploadToCDN()' }); AWS.config.update({ accessKeyId: app.config.AWS_ACCESS_KEY, secretAccessKey: app.config.AWS_SECRET_KEY, region: app.config.AWS_REGION }); let params: any = { Bucket: app.config.AWS_BUCKET_NAME, Key: targetPath, Body: buffer }; new AWS.S3().putObject(params, (err, data) => { if (err) { app.logger!.error({ className: 'AWSCdnuploads', message: `Error occured in CDN-UPLOAD-ENGINE`, propertyName: 'aws-cdn-upload', cause: err, targetPath: targetPath }); return reject(err); } else { app.logger!.debug({ className: 'AWSCdnuploads', propertyName: 'aws-cdn-upload-response', propertyValue: data }); return resolve((app.config.AWS_CDN_URL + targetPath)); } }); } catch (e) { app.logger!.error({ propertyName: 'aws-cdn-upload', className: 'AWSCdnuploads', message: `Error occured in CDN-UPLOAD-ENGINE`, cause: e, targetPath: targetPath }); return reject(e); } }); }