新增桶权限选择

This commit is contained in:
CN-JS-HuiBai
2026-04-02 19:32:26 +08:00
parent 2491988d23
commit f991125923
5 changed files with 158 additions and 18 deletions

View File

@@ -20,18 +20,44 @@ app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Configure AWS S3 Client
const s3Client = new S3Client({
region: process.env.AWS_REGION || 'us-east-1',
endpoint: process.env.S3_ENDPOINT,
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === 'true',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
});
const rawBucketAddress = process.env.S3_BUCKET_ADDRESS || process.env.S3_BUCKET_NAME || '';
let BUCKET_NAME = rawBucketAddress;
const parsedBucketUrl = rawBucketAddress.includes('://') ? (() => {
try {
const parsed = new URL(rawBucketAddress);
const name = parsed.pathname.replace(/^\/+/, '');
if (name) {
BUCKET_NAME = name;
}
return parsed.origin;
} catch (err) {
return undefined;
}
})() : undefined;
const defaultS3ClientConfig = {
region: process.env.AWS_REGION || 'us-east-1',
endpoint: process.env.S3_ENDPOINT || parsedBucketUrl,
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === 'true'
};
const createS3Client = (credentials) => {
const clientConfig = { ...defaultS3ClientConfig };
if (credentials && credentials.username && credentials.password) {
clientConfig.credentials = {
accessKeyId: credentials.username,
secretAccessKey: credentials.password
};
} else if (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) {
clientConfig.credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
};
}
return new S3Client(clientConfig);
};
const BUCKET_NAME = process.env.S3_BUCKET_NAME;
const progressMap = {};
const wsSubscriptions = new Map();
@@ -84,6 +110,15 @@ const shouldRetryWithSoftware = (message) => {
return /Cannot load libcuda\.so\.1|Could not open encoder before EOF|Error while opening encoder|Operation not permitted|Invalid argument/i.test(message);
};
const extractS3Credentials = (req) => {
const username = req.headers['x-s3-username'] || req.body?.username || '';
const password = req.headers['x-s3-password'] || req.body?.password || '';
return {
username: typeof username === 'string' ? username.trim() : '',
password: typeof password === 'string' ? password : ''
};
};
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
@@ -134,6 +169,8 @@ app.get('/api/videos', async (req, res) => {
return res.status(500).json({ error: 'S3_BUCKET_NAME not configured' });
}
const allObjects = [];
const auth = extractS3Credentials(req);
const s3Client = createS3Client(auth);
let continuationToken;
do {
@@ -168,6 +205,11 @@ app.get('/api/videos', async (req, res) => {
}
});
app.get('/api/config', (req, res) => {
const title = process.env.APP_TITLE || 'S3 Media Transcoder';
res.json({ title });
});
app.post('/api/reset-cache', (req, res) => {
try {
clearMp4Cache();
@@ -215,6 +257,8 @@ app.post('/api/transcode', async (req, res) => {
fs.mkdirSync(outputDir, { recursive: true });
// Get S3 stream
const auth = extractS3Credentials(req);
const s3Client = createS3Client(auth);
const command = new GetObjectCommand({
Bucket: BUCKET_NAME,
Key: key