8
n8n 中文网amn8n.com

递归 Google Drive 文件夹复制器(保留权限)

高级

这是一个File Management, Multimodal AI领域的自动化工作流,包含 22 个节点。主要使用 If, Set, Code, Filter, GoogleDrive 等节点。 递归 Google Drive 文件夹复制器(保留权限)

前置要求
  • Google Drive API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "54727bff0f5eeffb7bf9295509a4e3003a61b32ef52459f329ae99e7b5243230",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "4a36d370-4d43-4090-a8e0-6e2002550bfc",
      "name": "设置配置",
      "type": "n8n-nodes-base.set",
      "position": [
        -1328,
        7968
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "sourceFolderId",
              "value": "1RdenWIrighEr6S53e25CkwYmUa7jAFjq"
            },
            {
              "name": "targetFolderId",
              "value": "13BQRVwg2QFGFRJstRzUt0CoO5w8bpS1I"
            },
            {
              "name": "newFolderName",
              "value": "Duplicated Folder"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "4107d258-363c-4bf9-b5bf-3cfa2a8e7f8e",
      "name": "创建主文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1168,
        7968
      ],
      "parameters": {
        "name": "={{ $json.newFolderName }}",
        "driveId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.targetFolderId }}"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        },
        "resource": "folder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "5b2cdf23-9f31-4668-a5cc-0dd2d6b94129",
      "name": "开始递归处理",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        -1008,
        7968
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "{{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {
            "sourceFolderId": "={{ $node['Set Configuration'].json.sourceFolderId }}",
            "targetFolderId": "={{ $json.id }}"
          },
          "schema": [
            {
              "id": "sourceFolderId",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "sourceFolderId",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "targetFolderId",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "targetFolderId",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "59abdaa3-b57e-4c75-a07e-1b8e720fb5ac",
      "name": "主完成",
      "type": "n8n-nodes-base.set",
      "position": [
        -848,
        7968
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "Main folder duplication completed successfully"
            },
            {
              "name": "sourceFolder",
              "value": "={{ $node['Set Configuration'].json.sourceFolderId }}"
            },
            {
              "name": "newMainFolder",
              "value": "={{ $node['Create Main Folder'].json.id }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "62d4d9eb-7c1d-48c8-bf0b-55269ee4caeb",
      "name": "准备文件数据",
      "type": "n8n-nodes-base.set",
      "position": [
        -864,
        8448
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "originalFileId",
              "value": "={{ $node['Filter Files Only1'].json.id }}"
            },
            {
              "name": "originalPermissions",
              "value": "={{ JSON.stringify($node['Filter Files Only1'].json.permissions || []) }}"
            },
            {
              "name": "newFileId",
              "value": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "27f8d8ca-96ab-41e8-96db-33afdbd63959",
      "name": "递归自调用",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        -160,
        8656
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "{{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {
            "sourceFolderId": "={{ $json.originalFolderId }}",
            "targetFolderId": "={{ $json.newFolderId }}"
          },
          "schema": [
            {
              "id": "sourceFolderId",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "sourceFolderId",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "targetFolderId",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "targetFolderId",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "15343d75-5c61-4de7-9ce4-4250ceccc03d",
      "name": "层级完成",
      "type": "n8n-nodes-base.set",
      "position": [
        64,
        8464
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "Level processing completed"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "0221146d-05a2-4c8f-a812-8b7ebe3e6a1c",
      "name": "搜索文件夹内容1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1344,
        8544
      ],
      "parameters": {
        "filter": {},
        "options": {
          "fields": [
            "id",
            "name",
            "mimeType",
            "permissions"
          ]
        },
        "resource": "fileFolder",
        "returnAll": true,
        "queryString": "'{{ $json.sourceFolderId }}' in parents and trashed = false",
        "searchMethod": "query"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "b437a057-3931-4c8e-ab6b-fab0f9f117dc",
      "name": "仅筛选文件1",
      "type": "n8n-nodes-base.filter",
      "position": [
        -1184,
        8448
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.mimeType }}",
              "value2": "application/vnd.google-apps.folder",
              "operation": "notEqual"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ce118b58-cd56-4eaf-adea-e2e117eec17e",
      "name": "仅筛选文件夹1",
      "type": "n8n-nodes-base.filter",
      "position": [
        -1184,
        8640
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.mimeType }}",
              "value2": "application/vnd.google-apps.folder"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4b80da29-fc25-48db-8aed-0f8c5ef13526",
      "name": "复制文件1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1024,
        8448
      ],
      "parameters": {
        "name": "={{ $json.name }}",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "driveId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $node['Start'].json.targetFolderId }}"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        },
        "operation": "copy",
        "sameFolder": false
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "38835c99-1d5c-4571-a505-ac3a57db699b",
      "name": "检查文件权限1",
      "type": "n8n-nodes-base.if",
      "position": [
        -704,
        8448
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ JSON.parse($json.originalPermissions || '[]').length }}",
              "value2": "0",
              "operation": "larger"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9c25ddac-17de-45c9-b106-e13e740f8385",
      "name": "准备文件权限1",
      "type": "n8n-nodes-base.code",
      "position": [
        -512,
        8272
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst newFileId = data.newFileId;\nconst originalPermissions = JSON.parse(data.originalPermissions || '[]');\nconst permissionsToApply = [];\nif (originalPermissions.length > 0) {\n  for (const permission of originalPermissions) {\n    if (permission.role !== 'owner' && permission.type && permission.role) {\n      const permObj = {\n        fileId: newFileId,\n        type: permission.type,\n        role: permission.role\n      };\n      if (permission.type === 'user' || permission.type === 'group') {\n        if (permission.emailAddress) {\n          permObj.emailAddress = permission.emailAddress;\n        }\n      } else if (permission.type === 'domain') {\n        if (permission.domain) {\n          permObj.domain = permission.domain;\n        }\n      }\n      permissionsToApply.push(permObj);\n    }\n  }\n}\nif (permissionsToApply.length > 0) {\n  return permissionsToApply.map(p => ({ json: p }));\n} else {\n  return [];\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "b2927eae-9f20-49ab-a60b-8fc5e6784cdb",
      "name": "应用文件权限1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -352,
        8272
      ],
      "parameters": {
        "fileId": "={{ $json.fileId }}",
        "options": {
          "sendNotificationEmail": false
        },
        "operation": "share",
        "permissionsUi": {
          "permissionsValues": {}
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "bc443b2f-cc4b-4474-a78c-1d2ec1e4f500",
      "name": "创建子文件夹1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1024,
        8640
      ],
      "parameters": {
        "name": "={{ $json.name }}",
        "driveId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $node['Start'].json.targetFolderId }}"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        },
        "resource": "folder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "01063a74-b9a6-4378-95b3-4dce49690f6f",
      "name": "准备文件夹数据1",
      "type": "n8n-nodes-base.set",
      "position": [
        -864,
        8640
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "originalFolderId",
              "value": "={{ $node['Filter Folders Only1'].json.id }}"
            },
            {
              "name": "originalPermissions",
              "value": "={{ JSON.stringify($node['Filter Folders Only1'].json.permissions || []) }}"
            },
            {
              "name": "newFolderId",
              "value": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "d223bee5-58d6-4f38-8eab-9ec0a4101a08",
      "name": "检查文件夹权限1",
      "type": "n8n-nodes-base.if",
      "position": [
        -704,
        8640
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ JSON.parse($json.originalPermissions || '[]').length }}",
              "value2": "0",
              "operation": "larger"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e840b153-3754-4fb2-88b9-97a4e675e05c",
      "name": "准备文件夹权限1",
      "type": "n8n-nodes-base.code",
      "position": [
        -512,
        8544
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst newFolderId = data.newFolderId;\nconst originalPermissions = JSON.parse(data.originalPermissions || '[]');\nconst permissionsToApply = [];\nif (originalPermissions.length > 0) {\n  for (const permission of originalPermissions) {\n    if (permission.role !== 'owner' && permission.type && permission.role) {\n      const permObj = {\n        fileId: newFolderId,\n        type: permission.type,\n        role: permission.role\n      };\n      if (permission.type === 'user' || permission.type === 'group') {\n        if (permission.emailAddress) {\n          permObj.emailAddress = permission.emailAddress;\n        }\n      } else if (permission.type === 'domain') {\n        if (permission.domain) {\n          permObj.domain = permission.domain;\n        }\n      }\n      permissionsToApply.push(permObj);\n    }\n  }\n}\nif (permissionsToApply.length > 0) {\n  return permissionsToApply.map(p => ({ json: p }));\n} else {\n  return [];\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "f4a0f68a-947a-4f57-b3fd-18a5d819592b",
      "name": "应用文件夹权限1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -352,
        8544
      ],
      "parameters": {
        "fileId": "={{ $json.fileId }}",
        "options": {
          "sendNotificationEmail": false
        },
        "operation": "share",
        "permissionsUi": {
          "permissionsValues": {}
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "t01llYvLZwTdfjby",
          "name": "Google Drive account 3"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "c3779818-366b-4a85-9422-8c72d4da956f",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1856,
        7664
      ],
      "parameters": {
        "color": 3,
        "width": 368,
        "height": 1200,
        "content": "# Google Drive 文件夹复制器(带权限)"
      },
      "typeVersion": 1
    },
    {
      "id": "0720dc37-20be-486e-9e7e-6086927013d1",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1472,
        7664
      ],
      "parameters": {
        "width": 1792,
        "height": 480,
        "content": "## 从此开始"
      },
      "typeVersion": 1
    },
    {
      "id": "6fbd5544-e61b-4cfd-a982-271eed9f45db",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1472,
        8160
      ],
      "parameters": {
        "width": 1792,
        "height": 704,
        "content": "## 核心递归引擎"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Copy File1": {
      "main": [
        [
          {
            "node": "Prepare File Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Subfolder1": {
      "main": [
        [
          {
            "node": "Prepare Folder Data1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare File Data": {
      "main": [
        [
          {
            "node": "Check File Has Permissions1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Configuration": {
      "main": [
        [
          {
            "node": "Create Main Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Main Folder": {
      "main": [
        [
          {
            "node": "Start Recursive Processing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Files Only1": {
      "main": [
        [
          {
            "node": "Copy File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Folders Only1": {
      "main": [
        [
          {
            "node": "Create Subfolder1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Folder Data1": {
      "main": [
        [
          {
            "node": "Check Folder Has Permissions1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply File Permission1": {
      "main": [
        [
          {
            "node": "Level Completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Call to Self": {
      "main": [
        [
          {
            "node": "Level Completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Folder Contents1": {
      "main": [
        [
          {
            "node": "Filter Files Only1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter Folders Only1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply Folder Permission1": {
      "main": [
        [
          {
            "node": "Recursive Call to Self",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare File Permissions1": {
      "main": [
        [
          {
            "node": "Apply File Permission1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Recursive Processing": {
      "main": [
        [
          {
            "node": "Main Completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check File Has Permissions1": {
      "main": [
        [
          {
            "node": "Prepare File Permissions1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Level Completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Folder Permissions1": {
      "main": [
        [
          {
            "node": "Apply Folder Permission1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Folder Has Permissions1": {
      "main": [
        [
          {
            "node": "Prepare Folder Permissions1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Recursive Call to Self",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 文件管理, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量22
分类2
节点类型7
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流