--- title: "AWS 集成——获取所有 Tulip 数据表并写入 S3" slug: "aws-integration-fetch-all-tulip-tables-and-write-to-s3" updated: 2026-10-22T12:40:31Z published: 2026-10-22T12:40:31Z canonical: "support.tulip.co/aws-integration-fetch-all-tulip-tables-and-write-to-s3" --- > ## Documentation Index > Fetch the complete documentation index at: https://support.tulip.co/llms.txt > Use this file to discover all available pages before exploring further. # AWS 集成——获取所有 Tulip 数据表并写入 S3 *简化从 Tulip 向 AWS S3 提取数据的过程,以拓展更广泛的分析与集成机会* ## 目的 本指南将分步演示如何通过 Lambda 函数提取所有 Tulip 表中的数据,并写入 S3 存储桶。 这不仅限于基本的提取查询,还会遍历给定实例中的所有表;这非常适合用于每周的 ETL 任务(提取、转换、加载) 该 Lambda 函数可通过多种资源触发,例如 Event Bridge 定时器或 API Gateway 架构示例如下:![image.png](https://cdn.document360.io/7c6ff534-cad3-4fc8-9583-912c4016362f/Images/Documentation/image%28725%29.png) ## 设置 此集成示例需要以下内容: - 使用 Tulip Tables API(在“账户设置”中获取 API 密钥和密钥密文) - Tulip 数据表(获取数据表的唯一 ID) 总体步骤:1. 创建一个带有相关触发器(API Gateway、Event Bridge Timer 等)的 AWS Lambda 函数2. 确保使用了适当的 Lambda 层(例如 pandas 和 awswrangler 等),并且 Tulip Tables API 密钥具有足够的权限3. 使用以下示例获取 Tulip 表数据(可在 S3、Glue、Redshift 等任何场景中使用)```pythonimport jsonimport awswrangler as wrimport boto3from datetime import datetimeimport pandas as pdimport requestsimport os # 获取当前时间戳以生成唯一文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") bucket = os.getenv('bucket_name') # 将字典转换为字符串的函数 def dict_to_str(cell): if isinstance(cell, dict): return str(cell) return cell def query_table(table_id, base_url, api_header): offset = 0 function = f'/tables/{table_id}/records?limit=100&offset={offset}&includeTotalCount=false&filterAggregator=all' r = requests.get(base_url+function, headers=api_header) df = pd.DataFrame(r.json()) length = len(r.json()) while length > 0: offset += 100 function = f'/tables/{table_id}/records?limit=100&offset={offset}&includeTotalCount=false&filterAggregator=all' r = requests.get(base_url+function, headers=api_header) length = len(r.json()) df_append = pd.DataFrame(r.json()) df = pd.concat([df, df_append], axis=0) df = df.apply(lambda row: row.apply(dict_to_str), axis=1) return df # 创建函数 def write_to_s3(row, base_url, api_header, bucket): table = row['label'] id = row['id'] df = query_table(id, base_url, api_header) ``` path = f's3://{bucket}/{timestamp}/{table}.csv'# 将 DataFrame 以 CSV 格式写入 S3wr.s3.to_csv( df=df, path=path, index=False)print(f"已将 {table} 写入 {path}")return f"已将 {table} 写入 {path}" ``` def lambda_handler(event, context): api_header = {'Authorization' : os.getenv('tulip_api_basic_auth')} instance = os.getenv('tulip_instance') base_url = f'https://{instance}.tulip.co/api/v3' get_tables_function = '/tables' r = requests.get(base_url+get_tables_function, headers=api_header) table_names = pd.DataFrame(r.json()) ``` # 查询表函数table_names.apply(lambda row: write_to_s3(row, base_url, api_header, bucket), axis=1)return { 'statusCode': 200, 'body': json.dumps('已写入 S3!')} ``` ``` ## 用例与后续步骤 完成与 Lambda 的集成后,您可以使用 SageMaker 笔记本、QuickSight 或其他多种工具轻松分析数据。 **1. 缺陷**预测——在生产缺陷发生前予以识别,提高“一次做对”率;——识别影响质量的核心生产驱动因素,以便实施改进 **2. 质量成本优化——**发掘在不影响客户满意度的前提下优化产品设计的机会 **3. 生产能耗优化——**确定可调节的生产杠杆以实现能耗最优化 **4. 交付与计划预测及优化——**根据客户需求和实时订单计划优化生产排程 **5. 全球设备/生产线基准分析——**对同类设备或机器进行标准化基准分析 **6. 全球/区域数字化绩效管理——**整合数据以创建实时仪表盘 ```