我对Aws s3存储桶很新,我正在尝试动态上传不同文件夹创建的文件。即使我的脚本正常工作。我的s3存储桶创建0字节文件,并且名称等于上次上传的文件夹。
这是我的server.js
import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware'
import webpackHotMidleware from 'webpack-hot-middleware';
import bodyParser from 'body-parser';
import AWS from 'aws-sdk';
import open from 'open';
import uuid from 'uuid'
import webpackConfig from './webpack.config';
// Create an S3 client
AWS.config.loadFromPath('c.json');
var s3 = new AWS.S3();
// Create a bucket and upload something into it
let app = express();
app.use(bodyParser.json({limit:"50mb"}));
app.use(express.static('public'));
const compiler = webpack(webpackConfig)
app.use(webpackMiddleware(compiler,{
hot:true,
publicPath:webpackConfig.output.publicPath,
noInfo:true
}));
app.use(webpackHotMidleware(compiler));
app.post('/api',(req,res) => {
let imageuri = req.body.data.data_uri;
let filename = req.body.data.filename;
let filetype = req.body.data.filetype;
const Name = uuid.v1();
var bucketName = 'nok-sample/'+Name;
let buf = new Buffer(imageuri.replace(/^data:image\/\w+;base64,/, ""),'base64');
console.log(buf.length);
s3.createBucket({Bucket: bucketName}, function() {
var params = {Bucket: bucketName, Key: filename, Body: buf,ContentType:filetype,ContentLength:buf.length,ACL:'public-read'};
s3.putObject(params, function(err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to " + bucketName + "/" + filename);
let resimage = {
"awsUrl":`https://s3.amazonaws.com/${bucketName}/${filename}`
};
res.json(resimage);
console.log(resimage)
});
});
});
app.get('/*',(req,res) => {
res.sendFile(path.join(__dirname,'./index.html'))
});
app.listen(3000,() => {
open('http://localhost:3000')
});