yemot-recordings-backup
Version:
Automatic backup tool for Yemot HaMashiach call recordings to Amazon S3
83 lines (75 loc) • 2.06 kB
JavaScript
const AWS = require('aws-sdk');
const logger = require('./logger');
const config = require('./config');
class S3Client {
constructor() {
// Configure AWS SDK
AWS.config.update({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey,
region: config.aws.region
});
this.s3 = new AWS.S3();
this.bucket = config.aws.bucket;
}
/**
* Upload file to S3
* @param {Buffer} fileContent - File content
* @param {string} key - S3 object key (path)
* @returns {Promise<object>} S3 upload result
*/
async uploadFile(fileContent, key) {
try {
const params = {
Bucket: this.bucket,
Key: key,
Body: fileContent,
ContentType: this.getContentType(key)
};
const result = await this.s3.upload(params).promise();
logger.info(`Uploaded file to S3: ${key}`);
return result;
} catch (error) {
logger.error(`Failed to upload file to S3 (${key}): ${error.message}`);
throw error;
}
}
/**
* Check if a file exists in S3
* @param {string} key - S3 object key
* @returns {Promise<boolean>} True if exists
*/
async fileExists(key) {
try {
await this.s3.headObject({
Bucket: this.bucket,
Key: key
}).promise();
return true;
} catch (error) {
if (error.code === 'NotFound') {
return false;
}
throw error;
}
}
/**
* Get content type based on file extension
* @param {string} key - File path or name
* @returns {string} Content type
*/
getContentType(key) {
const ext = key.split('.').pop().toLowerCase();
const contentTypes = {
mp3: 'audio/mpeg',
wav: 'audio/wav',
ogg: 'audio/ogg',
aac: 'audio/aac',
m4a: 'audio/mp4',
json: 'application/json',
txt: 'text/plain'
};
return contentTypes[ext] || 'application/octet-stream';
}
}
module.exports = S3Client;