@gp_jcisneros/aws-utils
Version:
AWS SDK utilities for GreenPay microservices
158 lines (137 loc) • 4.61 kB
JavaScript
const { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
const { AWSError } = require('@gp_jcisneros/errors');
/**
* Utilities for AWS S3 operations
*/
class S3Utils {
constructor(region = process.env.AWS_REGION || 'us-east-1') {
this.client = new S3Client({ region });
}
/**
* Upload a file to S3
* @param {string} bucketName - Name of the S3 bucket
* @param {string} key - Object key (file path)
* @param {Buffer|string} data - File data
* @param {string} contentType - Content type (default: 'application/json')
* @returns {Promise<Object>} - Upload result
*/
async uploadFile(bucketName, key, data, contentType = 'application/json') {
try {
const command = new PutObjectCommand({
Bucket: bucketName,
Key: key,
Body: data,
ContentType: contentType
});
const result = await this.client.send(command);
return {
success: true,
etag: result.ETag,
versionId: result.VersionId,
location: `s3://${bucketName}/${key}`
};
} catch (error) {
// Si ya es un error personalizado, lo relanzamos
if (error instanceof AWSError) {
throw error;
}
throw new AWSError(`S3 upload failed for ${bucketName}/${key}: ${error.message}`, 'S3', 'UPLOAD_ERROR');
}
}
/**
* Download a file from S3
* @param {string} bucketName - Name of the S3 bucket
* @param {string} key - Object key (file path)
* @returns {Promise<Object>} - File data and metadata
*/
async downloadFile(bucketName, key) {
try {
const command = new GetObjectCommand({
Bucket: bucketName,
Key: key
});
const result = await this.client.send(command);
const data = await result.Body.transformToString();
return {
data,
contentType: result.ContentType,
lastModified: result.LastModified,
etag: result.ETag
};
} catch (error) {
// Si ya es un error personalizado, lo relanzamos
if (error instanceof AWSError) {
throw error;
}
throw new AWSError(`S3 download failed for ${bucketName}/${key}: ${error.message}`, 'S3', 'DOWNLOAD_ERROR');
}
}
/**
* Delete a file from S3
* @param {string} bucketName - Name of the S3 bucket
* @param {string} key - Object key (file path)
* @returns {Promise<Object>} - Delete result
*/
async deleteFile(bucketName, key) {
try {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key
});
const result = await this.client.send(command);
return {
success: true,
versionId: result.VersionId
};
} catch (error) {
// Si ya es un error personalizado, lo relanzamos
if (error instanceof AWSError) {
throw error;
}
throw new AWSError(`S3 delete failed for ${bucketName}/${key}: ${error.message}`, 'S3', 'DELETE_ERROR');
}
}
/**
* Upload JSON data to S3
* @param {string} bucketName - Name of the S3 bucket
* @param {string} key - Object key (file path)
* @param {Object} jsonData - JSON data to upload
* @returns {Promise<Object>} - Upload result
*/
async uploadJson(bucketName, key, jsonData) {
const data = JSON.stringify(jsonData, null, 2);
return this.uploadFile(bucketName, key, data, 'application/json');
}
/**
* Download JSON data from S3
* @param {string} bucketName - Name of the S3 bucket
* @param {string} key - Object key (file path)
* @returns {Promise<Object>} - JSON data
*/
async downloadJson(bucketName, key) {
const result = await this.downloadFile(bucketName, key);
return JSON.parse(result.data);
}
// Static methods for convenience
static async uploadFile(bucketName, key, data, contentType = 'application/json') {
const utils = new S3Utils();
return utils.uploadFile(bucketName, key, data, contentType);
}
static async downloadFile(bucketName, key) {
const utils = new S3Utils();
return utils.downloadFile(bucketName, key);
}
static async deleteFile(bucketName, key) {
const utils = new S3Utils();
return utils.deleteFile(bucketName, key);
}
static async uploadJson(bucketName, key, jsonData) {
const utils = new S3Utils();
return utils.uploadJson(bucketName, key, jsonData);
}
static async downloadJson(bucketName, key) {
const utils = new S3Utils();
return utils.downloadJson(bucketName, key);
}
}
module.exports = { S3Utils };