{
  "id": "OO8dBG01EPaQGcVA",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Create, Delete, and Organize AWS S3 Buckets & Files Directly from Your Email",
  "tags": [],
  "nodes": [
    {
      "id": "de49fbb0-5d1b-42ad-b500-beed9e895414",
      "name": "Start Workflow (GET Request)",
      "type": "n8n-nodes-base.emailReadImap",
      "position": [
        0,
        920
      ],
      "parameters": {
        "options": {
          "customEmailConfig": "[\"UNSEEN\", [\"SUBJECT\", \"s3\"]]"
        }
      },
      "credentials": {
        "imap": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "4ab408d8-416d-4a39-91cd-3e06553c36a1",
      "name": "Extract Data from Email",
      "type": "n8n-nodes-base.code",
      "position": [
        220,
        920
      ],
      "parameters": {
        "jsCode": "const emailBody = $json[\"textPlain\"] || \"\";\n\n// Helper function to match patterns\nfunction matchFirst(patterns) {\n  for (const pattern of patterns) {\n    const match = emailBody.match(pattern);\n    if (match) return match[1];\n  }\n  return null;\n}\n\n// Detect bucket name\nconst bucket = matchFirst([\n  /bucket[:\\s*]*([a-z0-9.-]+)/i,\n  /on bucket\\s+([a-z0-9.-]+)/i\n]);\n\n// Detect file name\nconst fileName = matchFirst([\n  /file[:\\s*]*([^\\s]+)/i,\n  /named\\s+([^\\s]+)/i\n]);\n\n// Detect source file (for copy)\nconst sourceFile = matchFirst([\n  /source\\s+([^\\s]+)/i\n]);\n\n// Detect destination file (for copy)\nconst destFile = matchFirst([\n  /destination\\s+([^\\s]+)/i\n]);\n\n// Decide task type\nlet task_type = null;\nif (/create bucket/i.test(emailBody)) task_type = \"create_bucket\";\nelse if (/delete bucket/i.test(emailBody)) task_type = \"delete_bucket\";\nelse if (/copy file/i.test(emailBody)) task_type = \"copy_file\";\nelse if (/delete file/i.test(emailBody)) task_type = \"delete_file\";\nelse if (/download file/i.test(emailBody)) task_type = \"download_file\";\nelse if (/upload file/i.test(emailBody)) task_type = \"upload_file\";\nelse if (/list files/i.test(emailBody)) task_type = \"get_files\";\n\nreturn [\n  {\n    json: {\n      bucket,\n      fileName,\n      sourceFile,\n      destFile,\n      task_type\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d2926988-4e41-4d5e-84e2-75893ed04565",
      "name": "Create a bucket",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        320
      ],
      "parameters": {
        "name": "new-project",
        "resource": "bucket",
        "additionalFields": {}
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "1401cf3e-62c1-44db-bca6-29231f3add1e",
      "name": "Delete a bucket",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        520
      ],
      "parameters": {
        "name": "new-project",
        "resource": "bucket",
        "operation": "delete"
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "43d9a1bb-6ad5-4916-91ef-0e9df365d8f4",
      "name": "Copy a file",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        720
      ],
      "parameters": {
        "operation": "copy",
        "sourcePath": "/old/old-project-image.jpg",
        "destinationPath": "/new/new-project-image.jpg",
        "additionalFields": {}
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "b7af67ad-ba3d-41fe-96fc-3613f332a1fa",
      "name": "Upload a file",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        1320
      ],
      "parameters": {
        "fileName": "/new/new-project-image.jpg",
        "operation": "upload",
        "bucketName": "new-project",
        "additionalFields": {}
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "e8290bcc-0816-421d-bea3-e588809d63b9",
      "name": "Get many files",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        1520
      ],
      "parameters": {
        "options": {},
        "operation": "getAll",
        "bucketName": "={{ $json.bucket }}"
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "cd585a21-6073-4a59-a157-943360266ac4",
      "name": "Download a file",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        1120
      ],
      "parameters": {
        "fileKey": "/new/new-project-image.jpg",
        "bucketName": "new-project"
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "33819696-4b09-45c8-b938-0a929281be26",
      "name": "Delete a file",
      "type": "n8n-nodes-base.awsS3",
      "position": [
        660,
        920
      ],
      "parameters": {
        "fileKey": "/new/new-project-image.jpg",
        "options": {},
        "operation": "delete",
        "bucketName": "new-project-image"
      },
      "credentials": {
        "aws": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "ae71cb97-4e9e-4733-a024-990045bed90f",
      "name": "Check Task Type",
      "type": "n8n-nodes-base.switch",
      "position": [
        440,
        836
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "99e9f0b2-e19b-45c5-a60e-317d772e6291",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "create_bucket"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "4e72b4c7-eed9-4039-84c7-773a4dee2eb6",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "delete_bucket"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "aef1b8b8-9175-4819-9cb8-87209e4b2954",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "copy_file"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a256941b-0882-4ee1-9126-491da78f5fb4",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "delete_file"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a502de56-2efa-4766-b883-65d8cefaab1a",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "download_file"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "62aae8fd-71f2-4194-af78-b8d42179a2d0",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "upload_file"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "e70fb51d-7d2e-4ed2-bdde-7edd03059033",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.task_type }}",
                    "rightValue": "get_files"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "a3b10fac-ff46-4aac-9683-949c4c6246c8",
      "name": "Send Failed Email",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        1100,
        1020
      ],
      "parameters": {
        "text": "=AWS S3 Operation Failed",
        "options": {
          "replyTo": "={{ $('Start Workflow (GET Request)').item.json.from }}"
        },
        "subject": "=AWS S3 Operation Failed",
        "toEmail": "={{ $('Start Workflow (GET Request)').item.json.from }}",
        "fromEmail": "user@example.com",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "7c01eb42-56e7-431e-ba13-9fa8b88cb84f",
      "name": "Send Success Email ",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        1100,
        820
      ],
      "parameters": {
        "text": "=AWS S3 Operation Successful",
        "options": {
          "replyTo": "={{ $('Start Workflow (GET Request)').item.json.from }}"
        },
        "subject": "=AWS S3 Operation Successful",
        "toEmail": "={{ $('Start Workflow (GET Request)').item.json.from }}",
        "fromEmail": "user@example.com",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "d79e2096-604a-441b-9697-6e308b3002f5",
      "name": "Check - Success or Fail",
      "type": "n8n-nodes-base.if",
      "position": [
        880,
        920
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "045a2b56-33db-45b3-8c14-ad7d0ba64db7",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.Key }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2,
      "alwaysOutputData": true
    },
    {
      "id": "d6872864-93fc-462a-8693-41377e283d37",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -720,
        380
      ],
      "parameters": {
        "width": 1000,
        "height": 140,
        "content": "\n## What is this workflow for?\n### This workflow enables the management of AWS S3 buckets and files by processing email commands. It supports creating and deleting buckets, uploading and downloading files, copying files, and listing files, with automated email responses for confirmation or failure."
      },
      "typeVersion": 1
    },
    {
      "id": "c19d98fb-6112-4886-807f-c78f5aead7be",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -680,
        760
      ],
      "parameters": {
        "color": 4,
        "width": 560,
        "height": 400,
        "content": "## How It Works\n- **Start Workflow (GET Request)** - Captures incoming email requests.\n- **Extract Data from Email** - Parses email content to extract S3 operation commands.\n- **Check Task Type** - Validates the type of task (e.g., create bucket, delete file).\n- **Create a Bucket** - Creates a new S3 bucket.\n- **Delete a Bucket** - Deletes an existing S3 bucket.\n- **Copy a File** - Copies a file within S3.\n- **Delete a File** - Deletes a file from S3.\n- **Download a File** - Downloads a file from S3.\n- **Upload a File** - Uploads a file to S3.\n- **Get Many Files** - Lists multiple files in a bucket.\n- **Check Success or Fail** - Determines the outcome of the operation.\n- **Send Success Email** - Sends a success confirmation email.\n- **Send Failed Email** - Sends a failure notification email."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2bdf2886-f43d-4a91-853c-f40359658edd",
  "connections": {
    "Copy a file": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete a file": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload a file": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many files": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Task Type": {
      "main": [
        [
          {
            "node": "Create a bucket",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete a bucket",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Copy a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Upload a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get many files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a bucket": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete a bucket": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download a file": {
      "main": [
        [
          {
            "node": "Check - Success or Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check - Success or Fail": {
      "main": [
        [
          {
            "node": "Send Success Email ",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Failed Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Data from Email": {
      "main": [
        [
          {
            "node": "Check Task Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Workflow (GET Request)": {
      "main": [
        [
          {
            "node": "Extract Data from Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}