Skip to content

Commit ab16ab8

Browse files
committed
Data migration from AWS S3 to Google Cloud Platform
1 parent cf07e73 commit ab16ab8

File tree

3 files changed

+70
-48
lines changed

3 files changed

+70
-48
lines changed

Diff for: backend/models/postItem.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ const ItemSchema = new Schema(
1717
sparse: true
1818
}
1919
},
20-
itemPictures: [{ img: { type: String, required: false } }],
20+
itemPictures: [
21+
{
22+
id: {
23+
type: String
24+
},
25+
name: {
26+
type: String
27+
}
28+
}
29+
],
2130
price: {
2231
type: Number,
2332
required: true,

Diff for: backend/routes/category.js

+56-45
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,85 @@ const express = require("express");
44
const router = express.Router();
55
const multer = require("multer");
66
const shortid = require("shortid");
7+
const upload = multer();
8+
const KeyPath = "/etc/secrets/service.json";
79
var aws = require("aws-sdk");
810
var multerS3 = require("multer-s3");
911
const path = require("path");
12+
const stream = require("stream");
1013
const accesskey = process.env.AWSACCESSKEY
1114
const awssecret = process.env.AWSSECRETACCESSKEY
1215
const awsbucket = process.env.AWSBUCKET
13-
var storage = multer.diskStorage({
14-
destination: function (req, file, cb) {
15-
cb(null, "./uploads");
16-
},
17-
filename: function (req, file, cb) {
18-
cb(null, shortid.generate() + "-" + file.originalname + "-" + Date.now());
19-
},
20-
});
2116

22-
var upload = multer({ storage });
17+
//AWS S3 BUCKET METHOD:
18+
19+
20+
// const AWS = new aws.S3({
21+
// accessKeyId: accesskey,
22+
// secretAccessKey: awssecret,
23+
// });
24+
// var uploadS3 = multer({
25+
// storage: multerS3({
26+
// s3: AWS,
27+
// bucket: awsbucket,
28+
// acl: "public-read",
29+
// metadata: function (req, file, cb) {
30+
// cb(null, { fieldName: file.fieldname });
31+
// },
32+
// key: function (req, file, cb) {
33+
// cb(null, shortid.generate() + "-" + file.originalname + "-" + Date.now());
34+
// },
35+
// }),
36+
// });
2337

24-
const AWS = new aws.S3({
25-
accessKeyId: accesskey,
26-
secretAccessKey: awssecret,
38+
//GCP IMAGE UPLOAD METHOD
39+
40+
const SCOPES = [process.env.SCOPES_0, process.env.SCOPES_1];
41+
const auth = new google.auth.GoogleAuth({
42+
keyFile: KeyPath,
43+
scopes: SCOPES
2744
});
28-
var uploadS3 = multer({
29-
storage: multerS3({
30-
s3: AWS,
31-
bucket: "agriassistbucket",
32-
acl: "public-read",
33-
metadata: function (req, file, cb) {
34-
cb(null, { fieldName: file.fieldname });
45+
console.log(auth);
46+
47+
const uploadToGoogleDrive = async (fileObject) => {
48+
const bufferStream = new stream.PassThrough();
49+
bufferStream.end(fileObject.buffer);
50+
const response = await google.drive({ version: "v3", auth }).files.create({
51+
media: {
52+
mimeType: fileObject.mimeType,
53+
body: bufferStream,
3554
},
36-
key: function (req, file, cb) {
37-
cb(null, shortid.generate() + "-" + file.originalname + "-" + Date.now());
55+
requestBody: {
56+
name: fileObject.originalname,
57+
parents: [process.env.GOOGLE_FOLDER_ID],
3858
},
39-
}),
40-
});
59+
fields: "id,name",
60+
});
61+
return response.data;
62+
};
4163

4264
router.post(
4365
"/postitem",
4466
requireSignin,
4567
userMiddleware,
46-
uploadS3.array("itemPictures"),
68+
// uploadS3.array("itemPicture") /* AWS S3 Bucket upload method calling */
69+
upload.any("itemPictures"),
4770
async (req, res) => {
4871
// console.log(req)
4972
console.log("Hitted the POST successfully ");
5073
try {
5174
console.log("try");
5275
const { ItemType, ItemName, Price, Quantity } = req.body;
5376
// console.log(req.files);
54-
var itemPictures = [];
55-
if (req.files.length > 0) {
56-
itemPictures = req.files.map((file) => {
57-
console.log(file.key);
58-
return { img: file.key };
59-
});
77+
console.log(ItemType);
78+
console.log(req.files);
79+
console.log(req.body);
80+
const { body, files } = req;
81+
console.log(body);
82+
let itemPictures = [];
83+
for (let f = 0; f < files.length; f += 1) {
84+
itemPictures.push(await uploadToGoogleDrive(files[f]));
6085
}
61-
6286
//Saving data to db
6387

6488
const newPost = await postitem.create({
@@ -72,20 +96,7 @@ router.post(
7296
console.log(newPost._id);
7397
await newPost.save();
7498
console.log("save completed");
75-
res.status(201).json({ newPost });
76-
77-
// newPost.save()
78-
// .then((item) => {
79-
// return res.status(201).json({ item });
80-
// })
81-
// .catch((err) => {
82-
// return res.status(400).json({ err });
83-
// })
84-
// // newPost.save((error, item) => {
85-
// if (error) return res.status(400).json({ error });
86-
// if (item) return res.status(201).json({ item });
87-
// });
88-
99+
res.status(201).json({ message: "done" });
89100
} catch (err) {
90101
console.log(err);
91102
res.status(401).json({

Diff for: frontend/src/Components/Feed.jsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ function Feed() {
9292
<Card style={{ backgroundColor: "#99F3BD", borderRadius: "25px" }} bsPrefix="item-card">
9393
<Card.Img style={{ borderRadius: "25px", width: "100%", height: "100%" }}
9494
variant="top"
95-
src={`${S3_BUCKET_URL}/${item.itemPictures[0].img}`}
95+
// src={`${S3_BUCKET_URL}/${item.itemPictures[0].img}`}
96+
src={`https://drive.google.com/uc?export=view&id=${item.itemPictures[0].id}`}
9697
/>
9798
<Card.Body bsPrefix="card-body" style={{ alignItems: "center" }}>
9899
<Card.Title
@@ -139,7 +140,8 @@ function Feed() {
139140
<Card style={{ backgroundColor: "#99F3BD", borderRadius: "25px" }} bsPrefix="item-card">
140141
<Card.Img style={{ borderRadius: "25px", width: "100%", height: "100%" }}
141142
variant="top"
142-
src={`${S3_BUCKET_URL}/${item.itemPictures[0].img}`}
143+
// src={`${S3_BUCKET_URL}/${item.itemPictures[0].img}`}
144+
src={`https://drive.google.com/uc?export=view&id=${item.itemPictures[0].id}`}
143145
/>
144146
<Card.Body bsPrefix="card-body" style={{ alignItems: "center" }}>
145147
<Card.Title

0 commit comments

Comments
 (0)