Hello,
I'm a beginner in nextjs and am trying desperately to read an mp4 from my s3 bucket and play it in the browser.
Unfortunately I always get the message "File not found, error 404".
The credentials are correct. There is no message about incorrect credentials.
I don't understand it anymore. 🥺
The api function looks like this:
import AWS from 'aws-sdk';
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
endpoint: process.env.S3_ENDPOINT,
s3ForcePathStyle: true,
});
export default async function handler(req, res) {
const params = {
Bucket: process.env.S3_BUCKET_NAME,
Key: 'vids/air.mp4',
};
try {
await s3.headObject(params).promise();
const data = await s3.getObject(params).promise();
const fileContent = data.Body.toString('utf-8');
res.status(200).json({ content: fileContent });
} catch (error) {
if (error.code === 'NotFound') {
console.error('File not found:', error);
res.status(404).json({ error: 'File not found' });
} else {
console.error('file read error:', error);
res.status(500).json({ error: 'file read error' });
}
}
}
The file exists and the path is also correct.
I can access the folder using the "commander one" software and can see the file.
The file is called as follows:
"use client";
import { useEffect, useState } from 'react';
export default function HeroHome() {
const [fileContent, setFileContent] = useState('');
useEffect(() => {
async function fetchFileContent() {
const response = await fetch('/api/readFile');
const data = await response.json();
setFileContent(data.content);
}
fetchFileContent();
}, []);
........
........
.... {fileContent}....
Can you please help me ? I'm really desperate....😢
Thanks