Encounter problem trying to upload all the attachment files from IMAP fetch email onto Google Drive

Describe the problem/error/question

I am new to n8n and trying to create a workflow to fetch email attachments and upload them to my Google Drive. So far, I can use Email Trigger (IMAP) Node to fetch email from my email account (non-Gmail). The test email has 3 pictures pasted inside the email content and one PDF file as an attachment. I can see the attachments as binary at the output of the IMAP node. I add the Code node after the IMAP node to split the attachment files into individual files using the following javascript. I can see the 1 item at inout and 4 items at output.

const binaryData = $binary;
const items = [];

// Loop through binary keys to extract attachment details
Object.keys(binaryData).forEach(key => {
    if (key.startsWith('attachment_')) { // Only process attachment keys
        items.push({
            json: {
                binaryKey: key, // Store the key for processing later
                fileName: binaryData[key].fileName, // File name of the attachment
            },
            binary: {
                [key]: binaryData[key], // Preserve binary data for processing
            },
        });
    }
});

return items; // Return items for the next node

However, when I added the Google Drive Upload file node at the Code node, I received the error message as below.

{
“errorMessage”: “The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined”,
“errorDetails”: {},
“n8nDetails”: {
“n8nVersion”: “1.72.1 (Self Hosted)”,
“binaryDataMode”: “default”,
“stackTrace”: [
“TypeError: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined”,
" at Function.from (node:buffer:320:9)“,
" at ExecuteContext.getItemBinaryData (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Google/Drive/v2/helpers/utils.js:44:30)”,
" at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Google/Drive/v2/actions/file/upload.operation.js:66:104)“,
" at ExecuteContext.router (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Google/Drive/v2/actions/router.js:58:83)”,
" at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Google/Drive/v2/GoogleDriveV2.node.js:16:38)“,
" at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:741:42)”,
" at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:724:66",
" at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:1155:20"
]
}
}

Sorry, I cannot share my workflow coz I am using a self-hosted n8n docker version. Hope the below-attached screenshots help you understand my issue and any constructive suggestion is very much welcome. Thank you very much for your time and support in advance.




Information on your n8n setup

  • **n8n version:**1.72.1
  • **Database (default: SQLite):**Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):**Docker self-hsot
  • Operating system: macOS (M2 Pro chip)

Welcome to the community @Clement_Woo !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


Sorry, I cannot share my workflow coz I am using a self-hosted n8n docker version.

Surely you can. it makes no difference how n8n instance is run. Just copy the workflow an d paste it in here as per tip above.

Splitting binaries into separate items is a valid approach. However, referencing the binary in GoogleDrive node is not correct. In your case you would have to use just “attachment_0”, “attachment_1”, etc. I believe that is your intention to use those variable names by adding them to .json.binaryKey.

The error rather indicates that something isn’t right with the items you created in the Code node. Try this instead

1 Like

@ihortom Thank you so much for your quick reply and guidance. The solution that you have provided helped resolve the issue and achieve the result that I wanted. Appreciate your support. :pray: :+1:

After evaluating alternative options, I think saving the attachment file into the local folder is much better. Because my n8n local environment is in the docker container, I think it’s more convenient to save the attachment files in a subdirectory of the mapped volume $(pwd)/n8n_storage. I added Read/Write from Disk behind the above Code node to split batch attachment with Operation option at 'Write File to Disk", the File Path ‘$(pwd)/n8n_storage/n8n_test/{{ Date.now() }}-{{ $json.fileName }}’ and Input Binary Field ‘data’. as shown below.

I got the error message:
{
“errorMessage”: “The file or directory does not exist [item 0]”,
“errorDetails”: {
“rawErrorMessage”: [
“ENOENT: no such file or directory, open ‘$(pwd)/n8n_storage/n8n_test/1736759759565-1.png’”,
“ENOENT: no such file or directory, open ‘$(pwd)/n8n_storage/n8n_test/1736759759565-1.png’”
],
“httpCode”: “ENOENT”
},
“n8nDetails”: {
“nodeName”: “Read/Write Files from Disk”,
“nodeType”: “n8n-nodes-base.readWriteFile”,
“nodeVersion”: 1,
“operation”: “write”,
“itemIndex”: 0,
“time”: “13/01/2025, 17:15:59”,
“n8nVersion”: “1.72.1 (Self Hosted)”,
“binaryDataMode”: “default”,
“stackTrace”: [
“NodeApiError: The file or directory does not exist”,
" at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Files/ReadWriteFile/actions/write.operation.js:100:19)“,
" at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Files/ReadWriteFile/ReadWriteFile.node.js:94:26)”,
" at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:741:19)“,
" at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:724:51”,
" at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:1155:20"
]
}
}

Not sure is because of the expression of the mapped volume. Need advice, thanks.