将以表单形式创建的文件(未从任何类型的文件存储中提取)上载到AWS S3 Bucket

时间:2017-09-14 13:14:45

标签: javascript html node.js heroku amazon-s3

我正在创建一个个人网站,允许我创建和保存文件到我的Amazon S3 Bucket。

设置如下:我有一个表单,其中包含文件名的文本输入和文件正文的文本区域。

这个想法:能够将文件保存为文件名和扩展名(即:如果文件名是'index.html',它将把它保存为我的存储桶中的html对象)。它将保存输入textarea的内容作为文件的正文内容。

错误: 1)POST heroku-url 500(内部服务器错误) 2)拒绝加载字体“数据:字体/ WOFF; BASE64,d09GRgABAAAAAGVUABEAAAAAxuQAAQABAAAAAAAAAAAAAAAAAAAAAAAAAABHREVGAAABgAAAAC4AAAA0ArgC7UdQT1MAAAGwAAAQ6AAALgxKsqRTR1NVQgAAEpgAAAH3AAAELqI5y + RPUy8yAAAUkAAAAE8AAABgaGyBu2NtYXAAABTgAAABlAAAAkQkRATXY3Z0IAAAFnQAAABeAAAAugDsQf1mcGdtAAAW1AAABZcAAAvNb3 / BHGdhc3AAABxsAAAACAAAAAgAAAAQZ2x5ZgAAHHQAAEApAAB3CtbiupxoZWFkAABcoAAAADYAAAA2BkubWWhoZWEAAFzYAAAAIAAAACQHFARfaG10eAAAXPgAAAI6AAAEEk4TN4Nsb2NhAABfNAAAAhIAAAISiLhpam1heHAAAGFIAAAAIAAAACACigzgbmFtZQAAYWgAAACUAAABHhQGLdJwb3N0AABh / AAAAq4AAASRk5y6n3ByZ ... QxUajCCFt4p9HP4fzdSWs2XhWl5HvJazrIrFUyB0l5dpqcW10lV2wukjMLuAvyMHNiYpgPsrCVXZDKrkpll6UWkh7kABVAFVCDe7UFmxagDegA + hLHRPbqtMo7ZHCpKdT6tPGXybzo0 + RXBLoPZt1tELcXxCmAAyZwYTJvdDFZKnDER44X2451rDqCyunIsRWvLSx6wnWqwPj / uX5 / KuEy6DL0z6A / Fn79VihxMFJsrlAFy4DpZOcvNlMeNp + BRDLj0r + XFdRxdSNSNxiI / AL3ojKdAAB4AWPw3sFwIihiIyNjX + QGxp0cDBwMyQUbGdictkUwWDAwsDJogTgOPN4c9iz6bMos4iysHFChUDZXJnMWTSZZJrAQt9M + YQYBBh4GTgY2kEZOoJiA0z4GBxiEiDEzuGxUYewIjNjg0BGxkTnFZaMaiLeLo4GBkcWhIzkkAqQ kEggceHw5HFkM2VRZJFlYebR2MP5v3cDSu5GJwWUDW9xG1hQXAFAmKZU ='因为它违反了以下内容安全策略指令:“default-src'self'”。请注意,'font-src'未明确设置,因此'default-src'用作后备。

注意:我正在使用Heroku,我使用'heroku config:set'命令来存储我的AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,并存储我的存储桶名称(S3_BUCKET)。

代码:

<h1>New File</h1>

<hr>

<form method="POST" action="/save-as">
  <input type="text" id="file_input" name="file_input" placeholder="File Name"><br>

  <textarea id="file_body" name="file_body"></textarea>

  <hr>
  <h2>Save changes</h2>

  <input id="submit" type="submit" value="Save">
</form>

const express = require('express');
const aws = require('aws-sdk');

var port = process.env.PORT || 3000;

const app = express();
app.set('views', './views');
app.use(express.static('./public'));
app.engine('html', require('ejs').renderFile);
app.listen(port);
console.log("App is listening on port: " + port);

aws.config.region = 'us-east-2';

const S3_BUCKET = process.env.S3_BUCKET;

app.get('/', (req,res) => res.render('index.html'));

app.post('/save-as', (req, res) => {
    const s3 = new aws.S3();
    const fileName = req.query['file_name'];
    const fileBody = req.query['file_body'];
    const s3Params = {
        Bucket: S3_BUCKET, 
        Key: fileName,
        Body: fileBody
    }
    s3.putObject(s3Params, (err,data) => {
       if(err) throw err;
        res.writeHead(200, {'Content-Type':'text/plain'});
        res.write("Successful!!");
        res.end();
    });
});

1 个答案:

答案 0 :(得分:0)

完全脱离解决错误的主题,但你可以把它写成console.log(`应用程序正在侦听端口:$ {port}`)那些反引号而不是对象文字的引号所以你不需要使用连接。