跳转到内容

S3 Writer Tool

S3WriterTool 专为将内容写入 Amazon S3 存储桶中的文件而设计。这个工具让 CrewAI agents 能在 S3 中创建或更新文件,非常适合需要存储数据、保存配置文件或将其他内容持久化到 AWS S3 存储的工作流。

要使用这个工具,需要安装所需依赖:

Terminal window
uv add boto3

要有效使用 S3WriterTool,请按以下步骤操作:

  1. 安装依赖:使用上面的命令安装所需包。
  2. 配置 AWS 凭据:将 AWS 凭据设置为环境变量。
  3. 初始化工具:创建该工具的实例。
  4. 指定 S3 路径和内容:提供要写入文件的 S3 路径以及要写入的内容。

下面的示例展示了如何使用 S3WriterTool 将内容写入 S3 存储桶中的文件:

from crewai import Agent, Task, Crew
from crewai_tools.aws.s3 import S3WriterTool
# Initialize the tool
s3_writer_tool = S3WriterTool()
# Define an agent that uses the tool
file_writer_agent = Agent(
role="File Writer",
goal="Write content to files in S3 buckets",
backstory="An expert in storing and managing files in cloud storage.",
tools=[s3_writer_tool],
verbose=True,
)
# Example task to write a report
write_task = Task(
description="Generate a summary report of the quarterly sales data and save it to {my_bucket}.",
expected_output="Confirmation that the report was successfully saved to S3.",
agent=file_writer_agent,
)
# Create and run the crew
crew = Crew(agents=[file_writer_agent], tasks=[write_task])
result = crew.kickoff(inputs={"my_bucket": "s3://my-bucket/reports/quarterly-summary.txt"})

当由 agent 使用时,S3WriterTool 接受以下参数:

  • file_path:必填。格式为 s3://bucket-name/file-name 的 S3 文件路径。
  • content:必填。要写入文件的内容。

该工具需要 AWS 凭据才能访问 S3 存储桶。你可以通过环境变量配置这些凭据:

  • CREW_AWS_REGION:S3 存储桶所在的 AWS 区域。默认值为 us-east-1
  • CREW_AWS_ACCESS_KEY_ID:你的 AWS access key ID。
  • CREW_AWS_SEC_ACCESS_KEY:你的 AWS secret access key。

在 agent 中使用 S3WriterTool 时,agent 需要同时提供 S3 文件路径和要写入的内容:

# Example of using the tool with an agent
file_writer_agent = Agent(
role="File Writer",
goal="Write content to files in S3 buckets",
backstory="An expert in storing and managing files in cloud storage.",
tools=[s3_writer_tool],
verbose=True,
)
# Create a task for the agent to write a specific file
write_config_task = Task(
description="""
Create a configuration file with the following database settings:
- host: db.example.com
- port: 5432
- username: app_user
- password: secure_password
Save this configuration as JSON to {my_bucket}.
""",
expected_output="Confirmation that the configuration file was successfully saved to S3.",
agent=file_writer_agent,
)
# Run the task
crew = Crew(agents=[file_writer_agent], tasks=[write_config_task])
result = crew.kickoff(inputs={"my_bucket": "s3://my-bucket/config/db-config.json"})

S3WriterTool 对常见的 S3 问题包含错误处理:

  • 无效的 S3 路径格式
  • 权限问题(例如没有写入存储桶的权限)
  • AWS 凭据问题
  • 存储桶不存在

发生错误时,工具会返回一条包含问题详情的错误消息。

S3WriterTool 使用 Python 的 AWS SDK(boto3)与 S3 交互:

class S3WriterTool(BaseTool):
name: str = "S3 Writer Tool"
description: str = "Writes content to a file in Amazon S3 given an S3 file path"
def _run(self, file_path: str, content: str) -> str:
try:
bucket_name, object_key = self._parse_s3_path(file_path)
s3 = boto3.client(
's3',
region_name=os.getenv('CREW_AWS_REGION', 'us-east-1'),
aws_access_key_id=os.getenv('CREW_AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('CREW_AWS_SEC_ACCESS_KEY')
)
s3.put_object(Bucket=bucket_name, Key=object_key, Body=content.encode('utf-8'))
return f"Successfully wrote content to {file_path}"
except ClientError as e:
return f"Error writing file to S3: {str(e)}"

S3WriterTool 提供了一种直接向 Amazon S3 存储桶中的文件写入内容的方式。通过让 agents 在 S3 中创建和更新文件,它为需要云端文件存储的工作流提供了支持。这个工具对于数据持久化、配置管理、报告生成以及任何涉及向 AWS S3 存储中保存信息的任务都非常有用。