# AWS - S3 Storage

> 在 Amazon S3 或兼容 S3 的存储上上传、下载、列出和删除对象。

Source: https://docs.sommatic.ai/zh/nodes/integrations.aws.s3.storage/

- 标识: `integrations.aws.s3.storage`
- 类别: 集成
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

在 Amazon S3 或兼容 S3 的存储上上传、下载、列出和删除对象。

连接你的 AWS 账户，自动化对象存储操作：存储文件、获取文档、列出存储桶内容，以及管理对象生命周期。

**操作：**
- **Get Object：** 按键下载对象内容。
- **Put Object：** 将内容上传到存储桶中的指定键。
- **List Objects：** 列出存储桶中的对象，可按前缀过滤。
- **Delete Object：** 按键删除对象。

## 配置

- `connection_id` (string, 必填): 兼容 S3 的存储账户凭据。接受 `object`（或 `basic-auth` / `custom`）类型的凭据，字段为 `{ accessKeyId, secretAccessKey, region?, endpoint? }`。AWS 不支持 `connection-string` 形式的凭据。
- `bucket` (string, 必填, 支持表达式): 要操作的 S3 存储桶名称。
- `operation` (string, 必填): 要执行的存储操作。 默认值: `getObject`. 可选值: `getObject`, `putObject`, `listObjects`, `deleteObject`.
- `key` (string, 可选, 支持表达式): 对象在存储桶中的路径/键（例如“reports/2024/summary.pdf”）。 显示条件: operation = `getObject` | `putObject` | `deleteObject`.
- `body` (string, 可选, 支持表达式): 作为对象正文上传的内容（字符串或 base64）。 显示条件: operation = `putObject`.

## 输出端口

- `out` (默认): 操作结果（内容、元数据或列表）
- `error`: 存储操作失败

## 输入契约

- `connection_id` (string, 必填)
- `bucket` (string, 必填)
- `operation` (string, 必填)
- `key` (string, 可选)
- `body` (string, 可选)

## 输出契约

- `result` (object, 可选): S3 操作结果或元数据

## 在表达式中

后续节点可通过 `$("AWS - S3 Storage").item.json.<路径>` 读取此节点输出中的内容（请把 `AWS - S3 Storage` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("AWS - S3 Storage").item.json.result` (object) = `"order_id,total\nord_123,129.99\n"`: S3 操作结果或元数据


## 工作原理

使用 AWS SDK v3 对兼容 S3 的存储桶执行一次 S3 操作（getObject、putObject、listObjects 或 deleteObject），并将 SDK 结果放在 `result` 中返回。

1. 解析节点配置中的表达式。若 `connection_id`、`bucket`、`operation`、`key` 或 `body` 为空，worker 会从输入项（`$json`）读取同名字段。
2. 必须提供 `connection_id`、`bucket` 和 `operation`。
3. 在服务端解析凭据。支持的类型：`object`、`basic-auth` 或 `custom`，需包含 `accessKeyId` 和 `secretAccessKey`（或 `access_key_id` / `secret_access_key`），可选 `region`（默认 `us-east-1`）和 `endpoint`。设置自定义 `endpoint` 后客户端改用 path-style 寻址。
4. 发送命令：`getObject` 读取 `key` 并以 UTF-8 字符串返回内容；`putObject` 将 `body` 上传到 `key`；`listObjects` 以 `key` 作为前缀列出存储桶；`deleteObject` 删除 `key`。
5. 成功时经 `out` 返回 `{ result }`。任何失败（包括校验错误）都以 `{ error }` 经 `error` 返回；节点不会抛出异常。

## 在工作流中

### 节点

```json
{
  "node_id": "node-read-report",
  "label": "Read daily report",
  "operator_slug": "integrations.aws.s3.storage",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "bucket": "reports",
    "operation": "getObject",
    "key": "daily/{{ $json.date }}.csv"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": "order_id,total\nord_123,129.99\n"
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

S3 调用成功。`getObject` 时 `result` 为对象内容字符串；其他操作时为 SDK 原始响应（例如 `listObjects` 的 `Contents`、`KeyCount`、`IsTruncated`，或 `putObject` 的 `ETag`）。

```json
{
  "result": "order_id,total\nord_123,129.99\n"
}
```

### `error`

校验、凭据解析或 S3 调用失败。

```json
{
  "error": "The specified key does not exist."
}
```

## 代码中的行为

- `key`: 在 `listObjects` 中作为 `Prefix` 使用（为空则列出整个存储桶），尽管表单只在其他操作中显示该字段。
- `body`: 按原样上传。base64 字符串会作为文本存储，worker 不会解码。
- `connection_id`: 以 `{` 开头的值会被解析为内联 JSON 设置（`accessKeyId`、`secretAccessKey`、`region`、`endpoint`），并跳过凭据库。仅用于本地测试；在工作流中请使用已保存的凭据。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `connection_id is required` | `error` | 配置和输入项中都没有凭据。`bucket is required` 与 `operation is required` 同理。 |
| `Unknown operation: <operation>` | `error` | `operation` 不是 getObject、putObject、listObjects 或 deleteObject 之一（区分大小写）。 |
| `Credential <connection_id> has unsupported type '<type>' for integrations.aws.s3.storage. Use 'object' with { accessKeyId, secretAccessKey, region?, endpoint? }.` | `error` | 凭据类型为 `connection-string` 或 `api-key`。请改存为 `object` 类型凭据。 |
| `Credential <connection_id> missing accessKeyId / secretAccessKey` | `error` | 凭据密钥中缺少这两个字段之一。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据不存在、对当前组织不可用，或无法连接后端。 |
| `The specified key does not exist.` | `error` | 对不存在的键执行 `getObject` 时的 S3 错误。其他 S3 错误（AccessDenied、NoSuchBucket）也按原样传递。 |

## 说明与限制

- `listObjects` 只返回一页（按 S3 API 最多 1,000 个键），不会发送续传令牌。对于大量数据，请检查 `IsTruncated` 并缩小前缀范围。
- `getObject` 会将内容按文本解码，二进制对象（PDF、图片）无法完整返回。
- 不重试：调用失败直接进入 `error`。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "integrations.aws.s3.storage",
  "name": "AWS - S3 Storage",
  "category": "integrations",
  "version": "1.0.0",
  "description": "Upload, download, list, and delete objects on Amazon S3 or S3-compatible storage.\n\n  Connect your AWS account and automate object storage operations — store files, retrieve documents, list bucket contents, and manage object lifecycle.\n\n  **Operations:**\n  - **Get Object:** Download an object's content by key.\n  - **Put Object:** Upload content to a specific key in the bucket.\n  - **List Objects:** List objects in a bucket with optional prefix filtering.\n  - **Delete Object:** Remove an object by key.",
  "capabilities": [
    "storage",
    "s3",
    "files",
    "aws",
    "cloud"
  ],
  "group": [
    {
      "key": "aws",
      "name": "AWS",
      "icon_url": "/icons/integrations/aws/aws.svg"
    }
  ],
  "icon_url": "/icons/integrations/aws/s3.svg",
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "AWS S3 Credential",
        "description": "Credential for your S3-compatible storage account. Accepts credentials of type `object` (or `basic-auth` / `custom`) with fields `{ accessKeyId, secretAccessKey, region?, endpoint? }`. AWS does not support `connection-string` style credentials.",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "object",
            "basic-auth",
            "custom"
          ],
          "types": [
            "s3"
          ]
        }
      },
      "bucket": {
        "type": "string",
        "title": "Bucket",
        "description": "Name of the S3 bucket to operate on.",
        "format": "expression"
      },
      "operation": {
        "type": "string",
        "title": "Operation",
        "enum": [
          "getObject",
          "putObject",
          "listObjects",
          "deleteObject"
        ],
        "default": "getObject",
        "description": "The storage operation to perform."
      },
      "key": {
        "type": "string",
        "title": "Object Key",
        "description": "Path/key of the object in the bucket (e.g., \"reports/2024/summary.pdf\").",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "getObject",
              "putObject",
              "deleteObject"
            ]
          }
        }
      },
      "body": {
        "type": "string",
        "title": "Content",
        "description": "Content to upload as the object body (string or base64).",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "putObject"
            ]
          }
        }
      }
    },
    "required": [
      "connection_id",
      "bucket",
      "operation"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "bucket": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "key": {
        "type": "string"
      },
      "body": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "bucket",
      "operation"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "S3 operation result or metadata"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "Operation result (content, metadata, or list)",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Storage operation failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "cloud-io"
  ],
  "tags": [
    "storage",
    "cloud",
    "aws"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://sommatic.ai",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "AWS S3 Credential",
      "description": "Credential for your S3-compatible storage account. Accepts credentials of type `object` (or `basic-auth` / `custom`) with fields `{ accessKeyId, secretAccessKey, region?, endpoint? }`. AWS does not support `connection-string` style credentials.",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "object",
          "basic-auth",
          "custom"
        ],
        "types": [
          "s3"
        ]
      }
    },
    "bucket": {
      "type": "string",
      "title": "Bucket",
      "description": "Name of the S3 bucket to operate on.",
      "format": "expression"
    },
    "operation": {
      "type": "string",
      "title": "Operation",
      "enum": [
        "getObject",
        "putObject",
        "listObjects",
        "deleteObject"
      ],
      "default": "getObject",
      "description": "The storage operation to perform."
    },
    "key": {
      "type": "string",
      "title": "Object Key",
      "description": "Path/key of the object in the bucket (e.g., \"reports/2024/summary.pdf\").",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "getObject",
            "putObject",
            "deleteObject"
          ]
        }
      }
    },
    "body": {
      "type": "string",
      "title": "Content",
      "description": "Content to upload as the object body (string or base64).",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "putObject"
          ]
        }
      }
    }
  },
  "required": [
    "connection_id",
    "bucket",
    "operation"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "bucket": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "key": {
      "type": "string"
    },
    "body": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "bucket",
    "operation"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "S3 operation result or metadata"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "Operation result (content, metadata, or list)",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Storage operation failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
