Reputation: 77
I am trying download a .rpm file from S3 and exract .rmp file using rpm-extract npm library. I am using sdk v2 and aws lambda. I am getting error while extracting the rmp files which is thrwon from here https://github.com/dowjones/rpm-extract/blob/master/src/gzUnzip.js#L15. I am not sure how the .rpm file in S3 bucket is compressed. But I am assuming that when I download the .rpm files from S3 and write to lambda /tmp folder, rpm file is getting compressed again. Is there a way I can directly download the file from S3 without reading and writing the content? I can't use sdk V3. I am using Nodejs aws lambda to run the code. or is there any other way to extract .rpm files in javascript. Here is code -
const AWS = require('aws-sdk');
const fs = require('fs');
const rpmExtract = require('rpm-extract')
// Create an S3 instance
const s3 = new AWS.S3();
const downloadFileFromS3 = async (bucket, key) => {
const tempFilePath = '/tmp/content.rmp'; // Store in /tmp directory
console.log('Temp file path set:', tempFilePath);
const params = {
Bucket: bucket,
Key: key
};
console.log('Params set for S3 getObject:', params);
console.log('Downloading file from S3...');
try {
const data = await s3.getObject(params).promise();
console.log('Writing file to disk...');
fs.writeFileSync(tempFilePath, data.Body);
console.log('File downloaded successfully:', tempFilePath);
return tempFilePath;
} catch (error) {
console.error('Error downloading file from S3:', error);
throw error;
}
};
async function getCertsFromRpm(s3Bucket, s3Key) {
try {
console.log("Inside getCertsFromRpm function");
const rpmFile = await downloadFileFromS3(s3Bucket, s3Key);
console.log("RPM file downloaded successfully:", rpmFile);
const files = await rpmExtract.default(rpmFile);
console.log("RPM file extracted successfully. Total files:", files.length);
// Filter out symbolic links and select only .pem files
const pemFiles = files
.filter((file) => !file.linkname && file.path.endsWith('.pem'))
.map((file) => file.data.toString());
console.log("Filtered PEM files:", pemFiles);
return pemFiles;
} catch (err) {
console.error('Error extracting certificates from RPM:', err);
return [];
}
}
module.exports = {
getCertsFromRpm
}
I tries using file stream too
const AWS = require('aws-sdk');
const fs = require('fs');
const rpmExtract = require('rpm-extract')
// Create an S3 instance
const s3 = new AWS.S3();
const downloadFileFromS3 = (bucket, key) => {
const tempFilePath = '/tmp/content.rpm'; // Store in /tmp directory
console.log('Temp file path set:', tempFilePath);
const params = {
Bucket: bucket,
Key: key
};
console.log('Params set for S3 getObject:', params);
console.log('Downloading file from S3...');
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(tempFilePath);
console.log('Writable file stream created.');
const s3Stream = s3.getObject(params).createReadStream();
console.log('S3 read stream created.');
s3Stream.on('error', (err) => {
console.error('Error retrieving file from S3:', err);
reject(err);
});
fileStream.on('error', (err) => {
console.error('Error writing file to disk:', err);
reject(err);
});
fileStream.on('close', () => {
console.log('File downloaded successfully:', tempFilePath);
resolve(tempFilePath);
});
console.log('Close listener added for file stream.');
s3Stream.on('data', (data) => {
console.log('Downloading data from S3...');
console.log('Data received:', data.toString()); // Log the data being written
});
console.log('Data listener added for S3 stream.');
s3Stream.on('end', () => {
console.log('S3 download stream ended.');
});
console.log('End listener added for S3 stream.');
s3Stream.pipe(fileStream);
console.log('Piped S3 stream to file stream.');
});
};
async function getCertsFromRpm(s3Bucket, s3Key) {
try {
console.log("Inside getCertsFromRpm function");
const rpmFile = await downloadFileFromS3(s3Bucket, s3Key);
console.log("RPM file downloaded successfully:", rpmFile);
const files = await rpmExtract(rpmFile);
console.log("RPM file extracted successfully. Total files:", files.length);
// Filter out symbolic links and select only .pem files
const pemFiles = files
.filter((file) => !file.linkname && file.path.endsWith('.pem'))
.map((file) => file.data.toString());
console.log("Filtered PEM files:", pemFiles);
return pemFiles;
} catch (err) {
console.error('Error extracting certificates from RPM:', err);
return [];
}
}
module.exports = {
getCertsFromRpm
}
Upvotes: 0
Views: 17