当我尝试将视频文件上传到S3存储桶时,需要花费大量时间来上传该文件。但是上传视频文件不是问题。但是上传时间非常长,但是我的视频文件大小仅为30MB。因此,这不是从应用程序上传视频文件的好方法。我必须将一些大型视频文件上传到S3存储桶中,每个视频可能需要200MB左右。因此,我需要一种更好的方法将这些视频文件上传到S3存储桶中。
这是我的代码段,
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
const fs = require('fs');
const fileType = require('file-type');
const bluebird = require('bluebird');
const multiparty = require('multiparty');
const cors = require('cors');
app.use(cors());
app.options('*', cors());
// configure the keys for accessing AWS
AWS.config.update({
accessKeyId: "xxx",
secretAccessKey: "yyy"
});
// configure AWS to work with promises
AWS.config.setPromisesDependency(bluebird);
// create S3 instance
const s3 = new AWS.S3();
// abstracts function to upload a file returning a promise
const uploadFile = (buffer, name, type) => {
const params = {
ACL: 'public-read',
Body: buffer,
Bucket: "bucket-name",
ContentType: type.mime,
Key: `${name}.${type}`
};
return s3.upload(params).promise();
};
// Define POST route
app.post('/test-upload', (request, response) => {
const form = new multiparty.Form();
response.header("Access-Control-Allow-Origin", "*");
form.parse(request, async (error, fields, files) => {
if (error) throw new Error(error);
try {
const path = files.file[0].path;
const buffer = fs.readFileSync(path);
const type = fileType(buffer);
const timestamp = Date.now().toString();
const fileName = `test/${timestamp}-lg`;
const data = await uploadile(buffer, fileName, type);
console.log("Data", data);
return response.status(200).send(data) +" " + data;
} catch (error) {
return response.status(400).send(error);
}
});
});
app.get('/', function(req, res){
res.send('Hello!')
})
var listener = app.listen(9000);
console.log('Server up and running...' + listener.address().address);
谢谢。