updating aws sdk to v3 - createPost now working

This commit is contained in:
Will Baumbach
2025-07-26 16:41:38 -05:00
parent 02d82bbde4
commit 61278967be
8 changed files with 1720 additions and 33 deletions

View File

@@ -1,38 +1,41 @@
import AWS from 'aws-sdk';
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { Buffer } from 'node:buffer';
import { v4 as uuidv4 } from 'uuid';
class AWSUtil {
constructor() {
this.s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
this.s3 = new S3Client({ region: "us-east-2" });
}
async uploadFile(base64, folder, ACL = "public-read") {
async uploadFile(base64, ACL = "public-read") {
const base64Data = Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
const type = base64.split(';')[0].split('/')[1];
const userId = 1;
const uuid = uuidv4();
const params = {
Bucket: process.env.AWS_S3_BUCKET,
Key: `${userId}.${type}`,
Key: `${uuid}.${type}`,
Body: base64Data,
ACL,
ContentEncoding: 'base64', // required
ContentType: `image/${type}` // required
};
// Create an object and upload it to the Amazon S3 bucket.
try {
const results = await this.s3.send(new PutObjectCommand(params));
console.log(
"Successfully created " +
params.Key +
" and uploaded it to " +
params.Bucket +
"/" +
params.Key
);
} catch (err) {
console.log("Error", err);
}
let location = '';
let key = '';
try {
const { Location, Key } = await this.s3.upload(params).promise();
location = Location;
key = Key;
} catch (error) {
console.log(error);
}
return {location, key}
return `https://tattletires.s3.us-east-2.amazonaws.com/${params.Key}`
}
async deleteFile(Location) {