Published 6 days ago
This template uses the item handling nodes, and expression-support in n8n, without using a Code
node, to extract multiple attachments from a GMail (trigger input) message/event, and (conditionally) upload each of them to Google Drive.
Note: There is another template titled Get Multiple Attachments from Gmail and upload them to GDrive that does basically the same thing, but it uses a Code
node.
Split Out
instead of Code
The “secret” to how this works is that n8n supports a special input field name $binary
that references the entire set of (multiple) binary data sub-elements in a single input item. It may look like an expression, but in this case it is a “fixed” (literal) value used as the Fields to Split Out
parameter value.
The next challenge with multiple attachments from a GMail message is that each one is still assigned different name like "attachment_0", "attachment_1", etc. This makes it tricky to reference them in a generic way. However, once n8n splits the items out, the binary in each item is always the first (i.e. index-zero / [0]
) and ONLY key/value. So, that makes it possible get the key-name and attributes of the corresponding value indirectly with some clever expression syntax.
Input Data Field Name
-> Expression: {{ $binary.keys()[0] }}
- This returns the name, regardless of whether it is "attachment_0", "attachment_1", or whatever else.{{ $binary.values()[0].fileName }}
{{ $binary.values()[0].fileExtension }}
{{ $binary.values()[0].fileType }}
{{ $binary.values()[0].fileSize }}
{{ $binary.values()[0].fileSize.split(' ')[0].toNumber() }}
{{ $binary.values()[0].mimeType }}
{{ $binary.values()[0].id }}
Since each of the attachments becomes a single item, it is relatively straightforward to introduce other n8n nodes like If
, Switch
, or Filter
and route each single attachment item into different workflow paths. The template demonstrates how each attachment binary could be routed based on its file size, as an example.