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 { Buffer } from 'node:buffer';
import { v4 as uuidv4 } from 'uuid';
class AWSUtil { class AWSUtil {
constructor() { constructor() {
this.s3 = new AWS.S3({ this.s3 = new S3Client({ region: "us-east-2" });
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
} }
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 base64Data = Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
const type = base64.split(';')[0].split('/')[1]; const type = base64.split(';')[0].split('/')[1];
const userId = 1; const uuid = uuidv4();
const params = { const params = {
Bucket: process.env.AWS_S3_BUCKET, Bucket: process.env.AWS_S3_BUCKET,
Key: `${userId}.${type}`, Key: `${uuid}.${type}`,
Body: base64Data, Body: base64Data,
ACL,
ContentEncoding: 'base64', // required ContentEncoding: 'base64', // required
ContentType: `image/${type}` // 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 = ''; return `https://tattletires.s3.us-east-2.amazonaws.com/${params.Key}`
let key = '';
try {
const { Location, Key } = await this.s3.upload(params).promise();
location = Location;
key = Key;
} catch (error) {
console.log(error);
}
return {location, key}
} }
async deleteFile(Location) { async deleteFile(Location) {

View File

@@ -11,7 +11,7 @@ import http from 'http';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import app from '../app.js'; import app from '../app.js';
const debug = debugLib('api:server'); const debug = debugLib('api:server');
dotenv.config({path: 'bin/.env'}); dotenv.config({path: '.env'});
mongoose mongoose
.connect(process.env.DB_CONNECTION).then(() => console.log('DB connection successful!')); .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(); const aws = new AWSUtil();
// Grab base64 photo from the req body // 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 = { const payload = {
...req.body, ...req.body,

23
api/package-lock.json generated
View File

@@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"aws-sdk": "^2.1692.0", "aws-sdk": "^2.1692.0",
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9", "debug": "~2.6.9",
"dotenv": "^17.2.1", "dotenv": "^17.2.1",
"express": "~4.16.1", "express": "~4.16.1",
@@ -413,6 +414,19 @@
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
"license": "MIT" "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": { "node_modules/css": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/css/-/css-1.0.8.tgz", "resolved": "https://registry.npmjs.org/css/-/css-1.0.8.tgz",
@@ -1331,6 +1345,15 @@
"node": ">= 0.6" "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": { "node_modules/on-finished": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",

View File

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

View File

@@ -11,7 +11,7 @@ export default function App() {
if (cameraRef.current) { if (cameraRef.current) {
const pic = await cameraRef.current.takePictureAsync() const pic = await cameraRef.current.takePictureAsync()
setPhoto(pic.base64 || ''); setPhoto(pic.base64 || '');
console.log(pic); console.log(pic.base64);
} }
} }
@@ -19,7 +19,7 @@ export default function App() {
const response = await fetch("localhost:3000/api/v1/posts", { const response = await fetch("localhost:3000/api/v1/posts", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
user: '6883ddb2640ebaa1a12e3791', userID: '6883ddb2640ebaa1a12e3791',
date: new Date(), date: new Date(),
photo: photo, photo: photo,
notes: '3333 W Smoochie St' notes: '3333 W Smoochie St'

1674
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,6 +11,7 @@
"lint": "expo lint" "lint": "expo lint"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.850.0",
"@expo/vector-icons": "^14.1.0", "@expo/vector-icons": "^14.1.0",
"@react-navigation/bottom-tabs": "^7.3.10", "@react-navigation/bottom-tabs": "^7.3.10",
"@react-navigation/elements": "^2.3.8", "@react-navigation/elements": "^2.3.8",
@@ -38,7 +39,8 @@
"react-native-safe-area-context": "5.4.0", "react-native-safe-area-context": "5.4.0",
"react-native-screens": "~4.11.1", "react-native-screens": "~4.11.1",
"react-native-web": "~0.20.0", "react-native-web": "~0.20.0",
"react-native-webview": "13.13.5" "react-native-webview": "13.13.5",
"uuid": "^11.1.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.25.2", "@babel/core": "^7.25.2",