s3文件上传未返回响应 [英] s3 file upload does not return response

查看:262
本文介绍了s3文件上传未返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node AWS-SDK将文件上传到现有的S3存储桶.使用下面的代码,文件最终会上传,但似乎几次都没有返回状态代码.另外,文件成功上传后,return语句也不会执行.

I'm using the Node AWS-SDK to upload files to an existing S3 bucket. With the code below, the file eventually uploads but it seems to return no status code a couple of times. Also, when the file successfully uploads, the return statement does not execute.

代码

exports.create = function(req, res) {
	var stream = fs.createReadStream(req.file.path);
	var params = {
		Bucket: 'aws bucket',
		Key: req.file.filename,
		Body: stream,
		ContentLength: req.file.size,
		ContentType: 'audio/mp3'
	};
	var s3upload = s3.upload(params, options).promise();
	
	s3upload
		.then(function(data) {
			console.log(data);
			return res.sendStatus(201);
		})
		.catch(function(err) {
			return handleError(err);
		});
}

日志

POST /api/v0/episode/upload - - ms - -
POST /api/v0/episode/upload - - ms - -
{ Location: 'https://krazykidsradio.s3-us-west-2.amazonaws.com/Parlez-vous%2BFrancais.mp3',
  Bucket: 'krazykidsradio',
  Key: 'Parlez-vous+Francais.mp3',
  ETag: '"f3ecd67cf9ce17a7792ba3adaee93638-11"' }

推荐答案

我知道了.请求超时时间不够长,无法完成上传,因此它再次发出了呼叫,依此类推.为了解决该问题,我将请求的超时设置为0,从而为请求提供了完成上传所需的所有时间.正确放置此位置后,它会正确地将201响应返回给客户端.

I figured it out. The request timeout was not long enough for the upload to finish, thus it was making the call again and so on and so on. To resolve the issue, I set the timeout for the request to 0, giving the request all the time it needs to finish the upload. With this in place, it properly returns a 201 response back to the client.

exports.create = function(req, res) {
    req.setTimeout(0); // <= set a create request to no timeout length.
    var stream = fs.createReadStream(req.file.path);
    var params = {
        Bucket: 'aws bucket',
        Key: req.file.filename,
        Body: stream,
        ContentLength: req.file.size,
        ContentType: 'audio/mp3'
    };
    var s3upload = s3.upload(params, options).promise();
    // return the `Promise`
    s3upload
        .then(function(data) {
            console.log(data);
            return res.sendStatus(201);
        })
        .catch(function(err) {
            return handleError(err);
        });
}

这篇关于s3文件上传未返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆