| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- const express = require('express');
- const aws = require('aws-sdk');
- const multer = require('multer');
- const multerS3 = require('multer-s3');
- const path = require("path");
- const app = express();
- // 配置 AWS SDK
- const s3 = new aws.S3({
- accessKeyId: 'AKIATLPEDU37QV5CHLMH',
- secretAccessKey: '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',
- region:'cn-northwest-1'
- });
- // 配置 Multer 以处理文件上传
- const upload = multer({
- storage: multerS3({
- s3: s3,
- bucket: 'ccrb',
- acl: 'public-read', // 设置文件权限为公共读取
- key: function (req, file, cb) {
- cb(null, file.fieldname + "-" + Date.now() + path.extname(file.originalname)); // 保存到S3时的文件名
- }
- })
- });
- // 接收文件上传的 POST 请求
- app.post('/img', upload.single('file'), (req, res) => {
- // console.log(req.file.location);
- res.json({ fileUrl: req.file.location }); // 返回上传后文件在S3中的URL
- });
- //app.use(router.routes()).use(router.allowedMethods());
- // 启动服务器
- const port = 7334;
- app.listen(port, () => {
- console.log(`Server is running on http://localhost:${port}`);
- });
|