Box Node SDK - upload issue with non-file read stream

SOLVED
Go to solution
First-time Contributor

Box Node SDK - upload issue with non-file read stream

My nodejs app has a client authenticated as an enterprise user. It works fine if I try to upload a file as below:

var fs = require("fs");
var stream = fs.createReadStream("z.txt");

boxClient.files
        .uploadFile(folderId, filename, stream)
        .catch(reason => console.log("Upload file error", reason));
 

 

However, I don't have a file but a stream. I tried with a simple readable stream and it fails:

const Readable = require("stream").Readable;
const s = new Readable();
s.push("testing");
s.push(null);

boxClient.files
        .uploadFile(folderId, filename, s)
        .catch(reason => console.log("Upload file error", reason));

 The error I get is:

"Error: Unexpected API Response [400 Bad Request] bad_request - Stream ended unexpectedly"

 

Is the SDK treating file streams differently or am I missing something?

3 REPLIES 3
Highlighted
Box Employee

Re: Box Node SDK - upload issue with non-file read stream

@raybirk In order to construct the API request, the internal networking library the SDK uses requires the stream to supply the amount of data it will provide (measured in bytes).  The allows the library to correctly calculate the `Content-Length` header for the request.  You can read more about this in a previously-reported issue on GitHub, which includes a helpful workaround for custom streams: https://github.com/box/box-node-sdk/issues/143

Highlighted
New Contributor

Re: Box Node SDK - upload issue with non-file read stream

@mwiller can you provide any guidance on _how_ you might indicate size?

 

For example, I have 

 

await client.files
.uploadFile(FOLDER_ID, fileName, fileToUpload, {size})

 

where size is the `size` in bytes of the `fileToUpload`
 
I've looked at the API docs (https://developer.box.com/reference/post-files-content) and don't see anything there. 
 
Thanks!
Highlighted
New Contributor

Re: Box Node SDK - upload issue with non-file read stream

Update -- you can't: https://github.com/box/box-node-sdk/issues/144

 

size is only available on the Chunked Upload