You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取Athena数据库中表的S3存储路径的代码需求

批量获取Athena数据库表的S3存储路径方案

直接解析DDL效率低下,推荐通过AWS Glue API读取表元数据来获取S3路径——因为Athena的表信息默认存储在Glue数据目录中,这种方式更准确高效。

实现步骤

1. 依赖安装

确保安装AWS SDK for Python(boto3):

pip install boto3

2. Python代码实现

import boto3
import csv

def get_athena_table_s3_paths(database_name):
    glue_client = boto3.client('glue')
    table_s3_paths = {}
    
    # 分页查询数据库下所有表
    paginator = glue_client.get_paginator('get_tables')
    for page in paginator.paginate(DatabaseName=database_name):
        for table in page['TableList']:
            table_name = table['Name']
            s3_path = table['StorageDescriptor']['Location']
            table_s3_paths[table_name] = s3_path
            print(f"{table_name}: {s3_path}")
    
    return table_s3_paths

if __name__ == "__main__":
    # 替换为你的目标Athena数据库名称
    target_db = "your_target_database"
    table_paths = get_athena_table_s3_paths(target_db)
    
    # 将结果保存到CSV文件(可选)
    with open('athena_table_s3_paths.csv', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(['表名', 'S3存储路径'])
        for name, path in table_paths.items():
            writer.writerow([name, path])

3. 权限与配置说明

  • 运行代码的身份需要具备glue:GetTables权限,可通过IAM策略配置
  • AWS凭证可通过以下方式配置:
    • 环境变量AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY
    • 本地~/.aws/credentials配置文件
    • EC2实例角色(如果在AWS云环境运行)

内容的提问来源于stack exchange,提问作者Chandrika Murthy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 08:55:22