如何使用Restify服务器将请求正文存储在GridFS中?

时间:2019-01-23 22:31:19

标签: node.js mongodb lodash gridfs restify

无法保存大于管道块大小(〜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

1 个答案:

答案 0 :(得分:0)

我在同一个团队中。 原来,我们正在深克隆包含流的对象。当我们停止克隆整个文件时,上传正常。