This commit is contained in:
CN-JS-HuiBai
2026-04-02 19:32:36 +08:00
parent f991125923
commit 1580b589c0
4 changed files with 248 additions and 39 deletions

View File

@@ -7,7 +7,7 @@ const path = require('path');
const http = require('http');
const WebSocket = require('ws');
const ffmpeg = require('fluent-ffmpeg');
const { S3Client, ListObjectsV2Command, GetObjectCommand } = require('@aws-sdk/client-s3');
const { S3Client, ListBucketsCommand, ListObjectsV2Command, GetObjectCommand } = require('@aws-sdk/client-s3');
dotenv.config();
@@ -162,11 +162,27 @@ const clearMp4Cache = () => {
}
};
// Endpoint to list available buckets
app.get('/api/buckets', async (req, res) => {
try {
const auth = extractS3Credentials(req);
const s3Client = createS3Client(auth);
const command = new ListBucketsCommand({});
const response = await s3Client.send(command);
const buckets = response.Buckets || [];
res.json({ buckets });
} catch (error) {
console.error('Error listing buckets:', error);
res.status(500).json({ error: 'Failed to list buckets', detail: error.message });
}
});
// Endpoint to list videos in the bucket
app.get('/api/videos', async (req, res) => {
try {
if (!BUCKET_NAME) {
return res.status(500).json({ error: 'S3_BUCKET_NAME not configured' });
const bucket = req.query.bucket || BUCKET_NAME;
if (!bucket) {
return res.status(400).json({ error: 'Bucket name is required' });
}
const allObjects = [];
const auth = extractS3Credentials(req);
@@ -175,7 +191,7 @@ app.get('/api/videos', async (req, res) => {
do {
const command = new ListObjectsV2Command({
Bucket: BUCKET_NAME,
Bucket: bucket,
ContinuationToken: continuationToken,
});
const response = await s3Client.send(command);
@@ -223,7 +239,13 @@ app.post('/api/reset-cache', (req, res) => {
// Endpoint to transcode S3 video streaming to MP4
app.post('/api/transcode', async (req, res) => {
const { key, codec, encoder } = req.body;
const { bucket, key, codec, encoder } = req.body;
if (!bucket) {
return res.status(400).json({ error: 'Bucket name is required' });
}
if (!key) {
return res.status(400).json({ error: 'Video key is required' });
}
if (!key) {
return res.status(400).json({ error: 'Video key is required' });
@@ -260,7 +282,7 @@ app.post('/api/transcode', async (req, res) => {
const auth = extractS3Credentials(req);
const s3Client = createS3Client(auth);
const command = new GetObjectCommand({
Bucket: BUCKET_NAME,
Bucket: bucket,
Key: key
});