Hey guys,
I am coming today because I am stuck on an upload feature i am trying to integrate. As usual, i am quite sure you will be able to help me out, so I open this topic for the next people struggling on uploading file to Google Cloud Storage.
So I followed this tutorial of yours (SQL + AWS S3) but i adapted it to MongoDB + GCS
Expected behavior
So what I should be able to is:
- For a given item, upload its picture
- While uploading the file, I want its
_id
file to be his name (i.e.: x1y2z3.jpg if the item id is x1y2z3)
Actual behavior
I created a smart action
forest/items.js
{
name: 'Upload item picture',
endpoint: '/forest/actions/items/upload-item-picture',
httpMethod: 'POST',
type: 'single',
fields: [{
field: 'Item picture',
description: 'The picture of the item in format xxxxxxxxx .........',
type: 'File',
isRequired: true
}]
}
I have my model
models/items.js
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');
// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({
// ...
'image_url': String,
// ...
}, {
timestamps: false,
});
module.exports = mongoose.model('items', schema, 'Items');
I also created the route:
routes/items.js
// Upload picture to gcs
router.post('/actions/upload-item-picture', permissionMiddlewareCreator.smartAction(), (req, res) => {
// Get the current item id
console.log("---------------------------------------------------------")
console.log("id: "+request.params.recordId)
res.status(204).send();
});
Failure Logs
When (in my smart action) I selected the file and then click on āUpload item pictureā, I get an error and the following output:
Unexpected error with Upload item picture action.
POST /forest/actions/items/upload-product-picture 413 63 - 1.130 ms
GET /forest/items/x1y2z3 304 - - 54.423 ms
Context
"@google-cloud/storage": "^5.7.4",
"@types/express": "^4.17.11",
"@types/forest-express-mongoose": "^6.3.3",
"@types/mongoose": "^5.10.3",
"@types/node": "^14.14.25",
"body-parser": "1.19.0",
"chalk": "~1.1.3",
"cookie-parser": "1.4.4",
"cors": "2.8.5",
"debug": "~4.0.1",
"dotenv": "~6.1.0",
"express": "~4.17.1",
"express-jwt": "5.3.1",
"forest-express-mongoose": "^6.0.0",
"mongoose": "~5.8.2",
"morgan": "1.9.1",
"nodemon": "^2.0.7",
"require-all": "^3.0.0",
"tsc-watch": "^4.2.9",
"typescript": "^4.1.3"
Note
Once it works, and if you agree, I would like to propose this topicās answer to be added to the doc as you did for SQL/AWS S3. Of course, the answer will have to be transformed into a proper documentation
Thanks for your help