如何使用 Restify 服务器将请求体存储在 GridFS 中?
How to store request body in GridFS using Restify server?
无法保存大于管道块大小 (~64K) 的文件。
使用mongodb 3.4.0,相关节点依赖
- 修复 4.2.0
- mongodb^2.2.12
lodash 4.16.6
bookeeppingData = {request, id, ...meta}
clone = lodash.cloneDeep(bookkeepingData)
const {
request: req,
id: _id,
meta: metadata,
} = clone
const bucket = new mongodb.GridFSBucket(
db,
{bucketName: 'my_gridfs_collection'}
);
const uploadSteam = bucket.openUploadStreamWithId(
_id,
undefined,
{metadata}
);
req.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
req.on('end', () => {
console.log('There will be no more data.');
});
req.on('error', (e) => {
console.log('req on error', e);
});
uploadSteam.on('finish', function() {
console.log('finsish');
keepThebooks(bookkeepingData);
});
uploadSteam.on('error', (e) => {
console.log('uploadstream on error', e);
});
req.pipe(uploadSteam);
}
发送小于~64K的文件时,控制台输出为
Received 57259 bytes of data.
There will be no more data.
finish
这是相应的 curl --verbose 输出(减去 json 响应):
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8060 (#0)
* Server auth using Basic with user 'driver'
> POST /artifact?branch=xyz&role=PHOTO&where.lat=55&where.long=77.2&where.acc=12&sys=system&id=1234&sys=system2&id=1234b&created=2018-11-01T18:41:50.850Z&tz=-600 HTTP/1.1
> Host: localhost:8060
> Authorization: Basic xxxxxxxxx
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: image/png
> Content-Length: 57259
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Type: application/json
< Content-Length: 388
< Date: Wed, 23 Jan 2019 20:58:16 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
发送大于~64K的文件时,控制台输出为:
Received 65536 bytes of data.
(就是这样 -- 没有错误)
对应的curl --verbose输出为:
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8060 (#0)
* Server auth using Basic with user 'driver'
> POST /artifact?branch=xyz&role=PHOTO&where.lat=55&where.long=77.2&where.acc=12&sys=system&id=1234&sys=system2&id=1234b&created=2018-11-01T18:41:50.850Z&tz=-600 HTTP/1.1
> Host: localhost:8060
> Authorization: Basic xxxxxxxxx
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: image/png
> Content-Length: 84801
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
我希望它能工作,并且控制台应该是这样的:
Received 65536 bytes of data.
Received 19274 bytes of data.
There will be no more data.
finish
我在同一个团队。
原来我们正在深度克隆包含流的对象。当我们停止克隆时,整个文件上传正常。
无法保存大于管道块大小 (~64K) 的文件。
使用mongodb 3.4.0,相关节点依赖
- 修复 4.2.0
- mongodb^2.2.12
lodash 4.16.6
bookeeppingData = {request, id, ...meta} clone = lodash.cloneDeep(bookkeepingData) const { request: req, id: _id, meta: metadata, } = clone const bucket = new mongodb.GridFSBucket( db, {bucketName: 'my_gridfs_collection'} ); const uploadSteam = bucket.openUploadStreamWithId( _id, undefined, {metadata} ); req.on('data', (chunk) => { console.log(`Received ${chunk.length} bytes of data.`); }); req.on('end', () => { console.log('There will be no more data.'); }); req.on('error', (e) => { console.log('req on error', e); }); uploadSteam.on('finish', function() { console.log('finsish'); keepThebooks(bookkeepingData); }); uploadSteam.on('error', (e) => { console.log('uploadstream on error', e); }); req.pipe(uploadSteam);
}
发送小于~64K的文件时,控制台输出为
Received 57259 bytes of data.
There will be no more data.
finish
这是相应的 curl --verbose 输出(减去 json 响应):
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8060 (#0)
* Server auth using Basic with user 'driver'
> POST /artifact?branch=xyz&role=PHOTO&where.lat=55&where.long=77.2&where.acc=12&sys=system&id=1234&sys=system2&id=1234b&created=2018-11-01T18:41:50.850Z&tz=-600 HTTP/1.1
> Host: localhost:8060
> Authorization: Basic xxxxxxxxx
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: image/png
> Content-Length: 57259
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Type: application/json
< Content-Length: 388
< Date: Wed, 23 Jan 2019 20:58:16 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
发送大于~64K的文件时,控制台输出为:
Received 65536 bytes of data.
(就是这样 -- 没有错误)
对应的curl --verbose输出为:
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8060 (#0)
* Server auth using Basic with user 'driver'
> POST /artifact?branch=xyz&role=PHOTO&where.lat=55&where.long=77.2&where.acc=12&sys=system&id=1234&sys=system2&id=1234b&created=2018-11-01T18:41:50.850Z&tz=-600 HTTP/1.1
> Host: localhost:8060
> Authorization: Basic xxxxxxxxx
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: image/png
> Content-Length: 84801
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
我希望它能工作,并且控制台应该是这样的:
Received 65536 bytes of data.
Received 19274 bytes of data.
There will be no more data.
finish
我在同一个团队。 原来我们正在深度克隆包含流的对象。当我们停止克隆时,整个文件上传正常。