How could I extract binary file url in webhook node?

Hi,

I get json from mailgun, with attach files. Node send data of attached files in binary data
But how could I extract these file or url?

You can use the Move Binary Data node for this:

I have this error:

Could I write to google drive directly?

What you are doing is to write to the hard-drive into the n8n folder. You will no have permission to do so (if you run it with docker) and that is why it fails.

If you want to write a file to Google Drive you should use the Google Drive node instead and set it to “Resource: File” and “Operation: Upload”. It should then work fine.

I tested with google drive, and it is ok

But how could I do with 2 files with 2 different property names (png and text…)?
It seems failed with 2 files or more

{
  "nodes": [
    {
      "parameters": {
        "url": "https://www.thecocktaildb.com/api/json/v1/1/random.php",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        590,
        150
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "position": [
        740,
        150
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "parents": [],
        "binaryData": true,
        "name": "test.json",
        "options": {}
      },
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        880,
        150
      ],
      "credentials": {
        "googleDriveOAuth2Api": "Google Drive OAuth2 API - [email protected]"
      }
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Where is the second file coming into play? Because your example only shows one.

It is generally possible to also upload multiple files. They just have to be in different items and you can then set their name via expression.

I get attachment from mailgun with webhook like this

But in fact, I do not know how to get 2 attachments or more

But I can not extract 2 binary files in google drive like your example
And I do not know how to extract the second in google drive node

{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "87f747ca-b46a-43ea-86db-45cc90597218",
        "responseMode": "lastNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -230,
        400
      ],
      "webhookId": "87f747ca-b46a-43ea-86db-45cc90597218"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "parents": [],
        "binaryData": true,
        "binaryPropertyName": "attachment-1",
        "name": "=test.png",
        "options": {}
      },
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        10,
        400
      ],
      "credentials": {
        "googleDriveOAuth2Api": "Google Drive OAuth2 API - [email protected]"
      },
      "disabled": true
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

In this case it is the easiest to branch of like this:
Screenshot from 2020-11-03 11-28-01
and upload them in two separate nodes.

Here the workflow example:

{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "87f747ca-b46a-43ea-86db-45cc90597218",
        "responseMode": "lastNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        750,
        400
      ],
      "webhookId": "87f747ca-b46a-43ea-86db-45cc90597218"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "parents": [],
        "binaryData": true,
        "binaryPropertyName": "attachment-1",
        "name": "test.png",
        "options": {}
      },
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        950,
        300
      ],
      "credentials": {
        "googleDriveOAuth2Api": "Google Drive OAuth2 API - [email protected]"
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "parents": [],
        "binaryData": true,
        "binaryPropertyName": "attachment-2",
        "name": "test.docx",
        "options": {}
      },
      "name": "Google Drive1",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        950,
        500
      ],
      "credentials": {
        "googleDriveOAuth2Api": "Google Drive OAuth2 API - [email protected]"
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          },
          {
            "node": "Google Drive1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Well, but users could send email with many attatched files
I do not want to limit by number of drive nodes

In this case, custom code is required. Code which iterates over all binary data and splits it into own items.

Here code for a Function-Node:

return Object.keys(items[0].binary).map(key => {
  return {
    json: {},
    binary: {
      data: items[0].binary[key],
    }
  }
});

The output of this node will be multiple items which all have one binary property called “data”. That property you can then use in the Google Drive node. n8n will then save all the files, one after another to Google Drive.

It works well with file name parameters
Thanks so much, you save my life!

Sure happy to help! Have fun!

Hi, now I have 2 function nodes to create data
How could I declare map to join to 1 function node?

2

Your array:

return Object.keys(items[0].binary).map(key => {
  return {
    json: {},
    binary: {
      data: items[0].binary[key],
    }
  }
});

My former function:

const text = items[0].json.body["From"];
if (text.includes("<") == false) {
var name = text;
var email = text;
} else {
const split = text.split('<')[0];
var name = split.trim();
var email_text = text.split('<')[1];
var email_text2 = email_text.split('>')[0];
var email_temp = email_text2.toLowerCase();
if (email_temp == null) {
var email = name;
} else {
var email = email_temp;
}
}
return [
  {
    json: { 
    name: name,
    email: email
    }
  }
]

That depends entirely on what kind of data you want in the end. So mainly on what you actually try to achieve. What is the workflow supposed to do that you are building right now?

Could I delare this into variable to return?

Object.keys(items[0].binary).map

like

var items = Object.keys(items[0].binary).map(key => {
  return {
    json: {},
    binary: {
      data: items[0].binary[key],
    }
  }
});

I do not know how to declare this into variable exactly