问题描述
我有一个AWS S3对象和在其上创建的读取流,如下所示:
I have an AWS S3 object and a read stream created on it like this:
const s3 = new AWS.S3();
const readStream = s3
.getObject(params)
.createReadStream()
.on('error', err => {
// do something
});
现在,当流在120秒后未读到末尾(例如,流媒体被客户端中止)时,将触发 error
事件,发生以下情况: TimeoutError:120000ms后连接超时
Now when the stream is not read to the end (e.g. the streaming is aborted by client) after 120 sec the error
event is triggered with: TimeoutError: Connection timed out after 120000ms
如何关闭流(或整个S3对象)?
How can I close the stream (or the entire S3 object)?
我尝试在此处中记录的 readStream.destroy()
a>,但是不起作用.
I tried readStream.destroy()
that is documented here, but it does not work.
推荐答案
我一直在寻找类似情况的解决方案,并碰到了这个线程.
I was looking for a solution to a similar case and bumped into this thread.
有一个 AWS请求中止
此处记录的方法,该方法可让您在不接收所有数据的情况下取消请求(这与节点的http请求类似).
There is an AWS Request abort
method documented here which allows you to cancel the request without receiving all the data (it's a similar concept to node's http request).
您的代码应如下所示:
const s3 = new AWS.S3();
const request = s3.getObject(params);
const readStream = request.createReadStream()
.on('error', err => {
request.abort(); // and do something else also...
});
可能是错误的,但就我而言-我正在获取数据,并且要在达到特定点时停止流传输(即,在文件中找到特定数据,这只是检查它是否是唯一的问题)存在-我什么都不需要了.
It may be on error, but in my case - I'm fetching data and I want to stop streaming when I've reached a certain point (i.e. found specific data in the file and it's only a matter of checking if it exists - I don't need anything else).
以上内容也可以与 request
和 node-fetch
模块一起很好地工作.
The above will work well with both request
and node-fetch
modules as well.
这篇关于如何关闭AWS S3读取流(AWSJavaScriptSDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!