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) {

View File

@@ -11,7 +11,7 @@ import http from 'http';
import mongoose from 'mongoose';
import app from '../app.js';
const debug = debugLib('api:server');
dotenv.config({path: 'bin/.env'});
dotenv.config({path: '.env'});
mongoose
.connect(process.env.DB_CONNECTION).then(() => console.log('DB connection successful!'));

View File

@@ -35,7 +35,7 @@ export const createPost = async (req, res, next) => {
const aws = new AWSUtil();
// Grab base64 photo from the req body
const {location} = aws.uploadFile(req.body.photo, 'photos');
const location = await aws.uploadFile(req.body.photo);
const payload = {
...req.body,

23
api/package-lock.json generated
View File

@@ -10,6 +10,7 @@
"dependencies": {
"aws-sdk": "^2.1692.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^17.2.1",
"express": "~4.16.1",
@@ -413,6 +414,19 @@
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
"license": "MIT"
},
"node_modules/cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"license": "MIT",
"dependencies": {
"object-assign": "^4",
"vary": "^1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/css": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/css/-/css-1.0.8.tgz",
@@ -1331,6 +1345,15 @@
"node": ">= 0.6"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",

View File

@@ -8,6 +8,7 @@
"dependencies": {
"aws-sdk": "^2.1692.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^17.2.1",
"express": "~4.16.1",