Feature(s) impacted
AWS S3 Upload
Observed behavior
The file picker widget insert a base64 file in my database and the upload on S3 bucket not working.
Expected behavior
An upload on S3 bucket, preview the image in the show page on the back-office and insert the path of my file in database.
Failure Logs
No failure logs
Code
main.ts (NestJS) :
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { createAgent } from '@forestadmin/agent';
import { createFileField } from '@forestadmin/plugin-aws-s3';
import { createSqlDataSource } from '@forestadmin/datasource-sql';
(async () => {
const app = await NestFactory.create(AppModule, { logger: false });
const agent = createAgent({
authSecret: process.env.FOREST_AUTH_SECRET,
envSecret: process.env.FOREST_ENV_SECRET,
isProduction: process.env.NODE_ENV === 'production',
typingsPath: './typings.ts',
typingsMaxDepth: 5,
});
app.enableCors({
origin: (origin, callback) => {
const allowedOrigins = [
'http://localhost:3000',
'http://localhost:3001',
'https://app.forestadmin.com',
];
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
});
agent.addDataSource(createSqlDataSource(process.env.DATABASE_URL));
const IMAGES_S3 = [
{
collection: 'ClubInformation',
field: 'logo',
},
{
collection: 'Partner',
field: 'image',
},
{
collection: 'Post',
field: 'image',
},
{
collection: 'Player',
field: 'image',
},
{
collection: 'Team',
field: 'image',
},
{
collection: 'StaffMember',
field: 'image',
},
{
collection: 'AboutPage',
field: 'image',
},
{
collection: 'AboutPage',
field: 'section2Image',
},
{
collection: 'AboutPage',
field: 'section3Image',
},
{
collection: 'PricingPage',
field: 'image',
},
{
collection: 'PricingPage',
field: 'section2Image',
},
{
collection: 'PricingPage',
field: 'section3Image',
},
{
collection: 'TrainingsPage',
field: 'image',
},
{
collection: 'TrainingsPage',
field: 'section2Image',
},
{
collection: 'TrainingsPage',
field: 'section3Image',
},
{
collection: 'HomePage',
field: 'heroImage',
},
{
collection: 'HomePage',
field: 'aboutImage',
},
{
collection: 'HomePage',
field: 'joinImage',
},
{
collection: 'HomePage',
field: 'whyJoinImage',
},
];
IMAGES_S3.forEach((data) => {
agent.customizeCollection(data.collection, (collection) =>
collection.use(createFileField, {
fieldname: data.field,
storeAt: (recordId, originalFilename) =>
`images/${recordId}/${originalFilename}`,
deleteFiles: false,
readMode: 'url',
acl: 'public-read',
aws: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_DEFAULT_REGION,
bucket: process.env.AWS_S3_BUCKET_NAME,
},
objectKeyFromRecord: null,
}),
);
});
await agent.mountOnNestJs(app).start();
await app.listen(3000);
})();
.env
AWS_ACCESS_KEY_ID="xxx"
AWS_SECRET_ACCESS_KEY="xxxx"
AWS_DEFAULT_REGION="eu-west-3"
AWS_S3_BUCKET_NAME="my-bucket-name"
Context
- Project name: Club Pongiste Quevillais
- Environment name: production and development
- Agent (forest package) name & version: “@forestadmin/agent”: “^1.27.1”,
- Database type: MySQL