updating aws sdk to v3 - createPost now working
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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!'));
|
||||
|
||||
@@ -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
23
api/package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -11,7 +11,7 @@ export default function App() {
|
||||
if (cameraRef.current) {
|
||||
const pic = await cameraRef.current.takePictureAsync()
|
||||
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", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
user: '6883ddb2640ebaa1a12e3791',
|
||||
userID: '6883ddb2640ebaa1a12e3791',
|
||||
date: new Date(),
|
||||
photo: photo,
|
||||
notes: '3333 W Smoochie St'
|
||||
|
||||
1674
package-lock.json
generated
1674
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,7 @@
|
||||
"lint": "expo lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.850.0",
|
||||
"@expo/vector-icons": "^14.1.0",
|
||||
"@react-navigation/bottom-tabs": "^7.3.10",
|
||||
"@react-navigation/elements": "^2.3.8",
|
||||
@@ -38,7 +39,8 @@
|
||||
"react-native-safe-area-context": "5.4.0",
|
||||
"react-native-screens": "~4.11.1",
|
||||
"react-native-web": "~0.20.0",
|
||||
"react-native-webview": "13.13.5"
|
||||
"react-native-webview": "13.13.5",
|
||||
"uuid": "^11.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.25.2",
|
||||
|
||||
Reference in New Issue
Block a user