in marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/sftp_upload.js [80:135]
async sendData(message, messageId, config) {
this.logger.debug(`Init SFTP uploader with Debug Mode.`);
let data;
try {
data = JSON.parse(message);
} catch (error) {
this.logger.info(`This is not a JSON string, SFTP uploading file not on GCS.`);
data = message;
}
let sftpClient = new SftpClient();
let output;
let targetFile;
if (data.bucket) {
const storageFile = new StorageFile(data.bucket, data.file);
output = storageFile.getFile().createReadStream();
const { fileName } = config;
if (fileName) {
targetFile = fileName;
} else {
// Working out a better file name for SFTP servers because:
// 1. SFTP servers have stricter rules then Cloud Storage, e.g. no colon.
// 2. By removing those tags (API, config, etc.) but keeping the value,
// the filenames shown in SA360 can be clearer.
targetFile = path.basename(data.file)
.replace(/API[\[|{]([\w-]*)[\]|}]/i, '$1')
.replace(/config[\[|{](\w*)[\]|}]/i, '$1')
.replace(/size\[(\w*)[\]|}]/i, '$1')
.replace(/:/g, '-');
}
} else {
output = Buffer.from(data);
targetFile = config.fileName || 'upload_by_tentacles_TIMESTAMP';
}
if (targetFile.indexOf(TIMESTAMP_PLACEHOLDER) > -1) {
const timestamp = new Date().toISOString().replace(/:/g, '.');
targetFile = targetFile.replace(TIMESTAMP_PLACEHOLDER, timestamp);
}
this.logger.debug(`Get message: ${message}`);
this.logger.debug(`SFTP Configuration: `, config);
this.logger.debug(`Output filename: ${targetFile}`);
/** @type {BatchResult} */ const batchResult = {};
try {
await sftpClient.connect(config.sftp);
this.logger.debug(`Open SFTP server: ${config.sftp.host}`);
const result = await sftpClient.put(output, targetFile);
this.logger.debug('SFTP operation: ', result);
this.logger.debug(`SFTP upload [${messageId}]: ${output}`);
await sftpClient.end();
batchResult.result = true;
} catch (error) {
batchResult.result = false;
batchResult.errors = [error.message];
this.logger.error('catch error', error);
}
return batchResult;
}