I encountered a consistent problem with using the AWS SDK in NextJS to upload images. I keep getting error code 403 (Forbidden). Could there be other reasons why this error is occurring besides the accessKeyId and secretAccessKey being invalid? Below is my basic code implementation:
const s3 = new S3({
accessKeyId: process.env.NEXT_PUBLIC_APP_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.NEXT_PUBLIC_APP_AWS_SECRET_ACCESS_KEY,
region: process.env.NEXT_PUBLIC_APP_AWS_REGION,
signatureVersion: 'v4',
});
export const uploadObject = async (
file: File,
key: string,
onProgress: (percent: number) => void,
onSuccess: () => void,
onError: () => void
) => {
const params = {
Bucket: process.env.NEXT_PUBLIC_APP_AWS_S3_BUCKET!,
Key: key,
Body: file,
ContentEncoding: 'base64',
ContentType: file.type,
};
s3.upload(params)
.on('httpUploadProgress', (evt) => {
onProgress(Math.ceil((100 * evt.loaded) / evt.total));
})
.send((error: any, data: any) => {
if (error) {
onError();
} else {
onSuccess();
}
});
};
Do you see any issues with my code?