cookbook icon indicating copy to clipboard operation
cookbook copied to clipboard

File upload example for javascript returns No file found in request.

Open LindaLawton opened this issue 1 year ago • 1 comments
trafficstars

I tried to run the file upload example for javascript

The file exits as i added a check for it

// Check if the file exists
if (!fs.existsSync(filePath)) {
    console.log(`File '${filePath}' does not exist` );
}

When i run the sample i get

GaxiosError: No file found in request. at Gaxios._request (C:\Development\Gemini\nodejs\gemini_samples_node\node_modules\gaxios\build\src\gaxios.js:144:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async JWT.requestAsync (C:\Development\Gemini\nodejs\gemini_samples_node\node_modules\google-auth-library\build\src\auth\oauth2client.js:405:18) at async run (C:\Development\Gemini\nodejs\gemini_samples_node\test2.js:30:36) { config: { url: 'https://generativelanguage.googleapis.com/upload/v1beta/files?uploadType=multipart', method: 'POST', userAgentDirectives: [ { product: 'google-api-nodejs-client', version: '7.1.0', comment: 'gzip' } ], paramsSerializer: [Function (anonymous)], data: PassThrough { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList { head: null, tail: null, length: 0 }, length: 0, pipes: [], flowing: false, ended: true, endEmitted: true, reading: false, constructed: true, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, errorEmitted: false, emitClose: true, autoDestroy: true, destroyed: true, errored: null, closed: true, closeEmitted: true, defaultEncoding: 'utf8', awaitDrainWriters: null, multiAwaitDrain: false, readingMore: false, dataEmitted: true, decoder: null, encoding: null, [Symbol(kPaused)]: true }, _events: [Object: null prototype] { prefinish: [Function: prefinish], error: [Function (anonymous)] }, _eventsCount: 2, _maxListeners: undefined, _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: true, needDrain: false, ending: true, ended: true, finished: true, destroyed: true, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: true, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: true, closeEmitted: true, [Symbol(kOnFinished)]: [] }, allowHalfOpen: true, _flush: [Function: flush], [Symbol(kCapture)]: false, [Symbol(kCallback)]: null }, headers: { 'x-goog-api-client': 'gdcl/7.1.0 gl-node/18.16.1', 'content-type': 'multipart/related; boundary=cf9e6ad4-9c0e-4e75-9a86-01de5c3e849f', 'Accept-Encoding': 'gzip', 'User-Agent': 'google-api-nodejs-client/7.1.0 (gzip)', 'X-Goog-Api-Key': '[REDACTED]' }, params: { uploadType: 'multipart' }, validateStatus: [Function (anonymous)], retry: true, body: PassThrough { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList { head: null, tail: null, length: 0 }, length: 0, pipes: [], flowing: false, ended: true, endEmitted: true, reading: false, constructed: true, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, errorEmitted: false, emitClose: true, autoDestroy: true, destroyed: true, errored: null, closed: true, closeEmitted: true, defaultEncoding: 'utf8', awaitDrainWriters: null, multiAwaitDrain: false, readingMore: false, dataEmitted: true, decoder: null, encoding: null, [Symbol(kPaused)]: true }, _events: [Object: null prototype] { prefinish: [Function: prefinish], error: [Function (anonymous)] }, _eventsCount: 2, _maxListeners: undefined, _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: true, needDrain: false, ending: true, ended: true, finished: true, destroyed: true, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: true, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: true, closeEmitted: true, [Symbol(kOnFinished)]: [] }, allowHalfOpen: true, _flush: [Function: flush], [Symbol(kCapture)]: false, [Symbol(kCallback)]: null }, responseType: 'unknown', errorRedactor: [Function: defaultErrorRedactor], retryConfig: { currentRetryAttempt: 0, retry: 3, httpMethodsToRetry: [ 'GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE' ], noResponseRetries: 2, statusCodesToRetry: [ [ 100, 199 ], [ 429, 429 ], [ 500, 599 ] ] } }, response: { config: { url: 'https://generativelanguage.googleapis.com/upload/v1beta/files?uploadType=multipart', method: 'POST', userAgentDirectives: [ { product: 'google-api-nodejs-client', version: '7.1.0', comment: 'gzip' } ], paramsSerializer: [Function (anonymous)], data: PassThrough { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList { head: null, tail: null, length: 0 }, length: 0, pipes: [], flowing: false, ended: true, endEmitted: true, reading: false, constructed: true, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, errorEmitted: false, emitClose: true, autoDestroy: true, destroyed: true, errored: null, closed: true, closeEmitted: true, defaultEncoding: 'utf8', awaitDrainWriters: null, multiAwaitDrain: false, readingMore: false, dataEmitted: true, decoder: null, encoding: null, [Symbol(kPaused)]: true }, _events: [Object: null prototype] { prefinish: [Function: prefinish], error: [Function (anonymous)] }, _eventsCount: 2, _maxListeners: undefined, _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: true, needDrain: false, ending: true, ended: true, finished: true, destroyed: true, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: true, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: true, closeEmitted: true, [Symbol(kOnFinished)]: [] }, allowHalfOpen: true, _flush: [Function: flush], [Symbol(kCapture)]: false, [Symbol(kCallback)]: null }, headers: { 'x-goog-api-client': 'gdcl/7.1.0 gl-node/18.16.1', 'content-type': 'multipart/related; boundary=cf9e6ad4-9c0e-4e75-9a86-01de5c3e849f', 'Accept-Encoding': 'gzip', 'User-Agent': 'google-api-nodejs-client/7.1.0 (gzip)', 'X-Goog-Api-Key': '[REDACTED]' }, params: { uploadType: 'multipart' }, validateStatus: [Function (anonymous)], retry: true, body: PassThrough { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList { head: null, tail: null, length: 0 }, length: 0, pipes: [], flowing: false, ended: true, endEmitted: true, reading: false, constructed: true, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, errorEmitted: false, emitClose: true, autoDestroy: true, destroyed: true, errored: null, closed: true, closeEmitted: true, defaultEncoding: 'utf8', awaitDrainWriters: null, multiAwaitDrain: false, readingMore: false, dataEmitted: true, decoder: null, encoding: null, [Symbol(kPaused)]: true }, _events: [Object: null prototype] { prefinish: [Function: prefinish], error: [Function (anonymous)] }, _eventsCount: 2, _maxListeners: undefined, _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: true, needDrain: false, ending: true, ended: true, finished: true, destroyed: true, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: true, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: true, closeEmitted: true, [Symbol(kOnFinished)]: [] }, allowHalfOpen: true, _flush: [Function: flush], [Symbol(kCapture)]: false, [Symbol(kCallback)]: null }, responseType: 'unknown', errorRedactor: [Function: defaultErrorRedactor] }, data: 'No file found in request.', headers: { 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', connection: 'close', 'content-length': '25', 'content-type': 'text/plain; charset=utf-8', date: 'Mon, 15 Apr 2024 12:38:14 GMT', server: 'UploadServer', 'x-guploader-uploadid': 'ABPtcPqNkgTHmrQKds3j_Ta0oLJiLONuY30frVrqh61kdQLK-YokDMT1RFx5H3EY38h3ivp7bvo' }, status: 400, statusText: 'Bad Request', request: { responseURL: 'https://generativelanguage.googleapis.com/upload/v1beta/files?uploadType=multipart' } }, error: undefined, status: 400, [Symbol(gaxios-gaxios-error)]: '6.4.0' }

Update

I did find that this works loading just the file path.

const media = {
        mimeType: mime.lookup(filePath),
        body: filePath,
    };

It seams its the loading the stream thats not working.

LindaLawton avatar Apr 15 '24 12:04 LindaLawton

Hi Linda - thanks for reporting this. I'd like to understand the issue with fs.createReadStream a bit more. Can you try running the sample code from the repo and see if that succeeds? If not can you provide the error and what env you're running with? readme

TYMichaelChen avatar Apr 15 '24 14:04 TYMichaelChen

Hey @LindaLawton - could you provide @TYMichaelChen with any more info?

markmcd avatar Aug 27 '24 23:08 markmcd

sorry @markmcd I havent touched this since then. I dont use env vars as there is really no need when you are passing the api key directly.

 API_KEY = process.env.GOOGLE_API_KEY;

LindaLawton avatar Aug 28 '24 16:08 LindaLawton