Loop over items or binary - how to get an "execution rank"


My workflow starts with an email trigger with 3 attachments.
Each of these attachments should be pushed to an s3 bucket, with a filename which is the email subject concatenated with the attachment rank (0, 1, 2…)

This workflow works fine when I duplicate the s3 node 3 times, and set the file name to: {{$node[“IMAP Email1”].json[“subject”]}}k - hardcoding the k-th attachment number in the s3 file name.

Now, how do I make this generic, i.e. working for any number of attachment?

Can I assume the s3 node will run as many times as there are input items (I’ve seen a post thanks to which I can transform one item with 3 binaries into 3 items, that part is covered) - and if it’s the case, how can I access a number that tells me which execution I’m at (0, 1, 2, …), so that I can include that in the file name? Or should I use a loop approach, explicitly setting a variable and incrementing it (in which case, to go out of the loop, I still need to access something like the items count (3), and I also don’t know how to do that…)

Thanks for your help, I’m new to n8n and a bit lost.

Hi @Udexia_Ridl, welcome to the community!

Check out this example workflow transforming one item with different binary properties into multiple items with a single and predictable binary property: Split Out Binary Data | n8n workflow template. The S3 node would run for each of these items.

The Function will also keep the filename, so you can use this in you subsequent nodes.

Wow, thanks for the blazingly fast answer!

Unfortunately, this function set the 3 created items names to “data”, it seems?

I don’t really need attachment filenames (as they are all the same) but rather I’d like to name my s3 file from the email subject + an int 0, 1, 2… (what comes after the “_” in “attachment_xx”). I was thinking, if the s3 node runs several times, can I simply access the “rank” of the execution, i.e. “it’s the k-th run of the node” => I access k?

Yes, pretty much. You can now connect your S3 node to the Function node - it should then run once for each item coming from the Function node and simply use the single data property:

You would need to adjust either the expression in your S3 node or the Function code a bit to include the email subject if you want to use it in your S3 node, but the basic idea should work :slight_smile:

yes, or I can indeed change the filenames and use your solution :sweat_smile:
thanks a lot!

I don’t think this will work unfortunately - the S3 node seems to expect only a single file. So you’d need to use multiple S3 nodes in this case (one for each attachment).

You are most welcome - just make sure to use an expression like {{ item(0).$node["Example Node"]... }} to make sure to always read the first item from your email node when going down the expression route (otherwise n8n would try to look up the second item from your email node when processing the second file to upload which won’t exist).

1 Like