我的nodejs应用程序在我的本地没有serverless的情况下工作得很好。但是当我使用serverless.yml配置部署相同的应用程序时,我得到了这个错误“内部服务器错误”。我试图在我的s3bucket中上传文件。我不确定我在我的serverless中是否缺少任何权限相关的代码。请帮助。
我的serverless.yml
service: nodejss3uploader
# Check out our docs for more details
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs16.x
memorySize: 2048
stage: prod
timeout: 15
region: us-east-1
iam:
role:
statements:
# Allow functions to list all buckets
- Effect: Allow
Action: 's3:ListBucket'
Resource: '*'
# Allow functions to read/write objects in a bucket
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:PutObject'
Resource:
- 'arn:aws:s3:::bucket-assets/*'
functions:
uploadtoS3:
handler: handler.uploadtoS3
events:
- http:
path: upload/aws
method: POST
environment:
AWS_ACCESS_ID: 123654123654123
AWS_SKEY: awesomeawskeypackage
AWS_S3_BUCKET: bucket-assets
处理程序. js代码
'use strict';
const app = require('./bin/www');
const serverless = require('serverless-http');
module.exports.handler = serverless(app);
www.js
码
var app = require('../app');
var debug = require('debug')('fileuploader:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '80');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
型
1条答案
按热度按时间dldeef671#
我已经设法解决了这个问题。
我不得不将我的
hander.js
代码更改为。我导入了app.js文件,而不是www.js
。app.js代码具有需要执行的函数。app.js
和serverless.yml没有任何变化。这样我就能够执行。