個案研究:建立報表儲存庫

Python 中的 AWS Boto 入門

Maksim Pecherskiy

Data Engineer

最終成果

最終成果

Python 中的 AWS Boto 入門

步驟

準備資料
  • 從原始資料 bucket 下載該月檔案
  • 串接為單一 CSV
  • 建立彙總的 DataFrame
Python 中的 AWS Boto 入門

步驟

產生報表
  • 輸出 DataFrame 為 CSV 與 HTML
  • 產生 Bokeh 圖,存為 HTML
Python 中的 AWS Boto 入門

步驟

上傳報表至可分享網站
  • 建立 gid-reports bucket
  • 將該月 3 個檔案上傳到 S3
  • 產生列出所有檔案的 index.html
  • 取得網站 URL!
Python 中的 AWS Boto 入門

原始資料 bucket

原始資料 bucket

  • 私人檔案
  • App 請求的每日 CSV
  • 原始資料

Gid requests bucket

Python 中的 AWS Boto 入門

讀取原始資料檔

# Create list to hold our DataFrames
df_list = []

# Request the list of csv's from S3 with prefix; Get contents response = s3.list_objects( Bucket='gid-requests', Prefix='2019_jan')
# Get response contents request_files = response['Contents']
Python 中的 AWS Boto 入門

讀取原始資料檔

# Iterate over each object
for file in request_files:
    obj = s3.get_object(Bucket='gid-requests', Key=file['Key'])

# Read it as DataFrame obj_df = pd.read_csv(obj['Body'])
# Append DataFrame to list df_list.append(obj_df)
Python 中的 AWS Boto 入門

讀取原始資料檔

# Concatenate all the DataFrames in the list
df = pd.concat(df_list)

# Preview the DataFrame df.head()

DataFrame 預覽

Python 中的 AWS Boto 入門

建立彙總報表

  • 執行彙總處理
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

彙總報表

Python 中的 AWS Boto 入門

報表 bucket

報表 bucket

  • Bucket 網站
  • 可公開存取
  • 彙總資料與 HTML 報表

報表 Bucket

Python 中的 AWS Boto 入門

上傳彙總 CSV

# Upload Aggregated CSV to S3
s3.upload_file(Filename='./jan_final_report.csv', 
                      Key='2019/jan/final_report.csv', 
                      Bucket='gid-reports',
                      ExtraArgs = {'ACL': 'public-read'})
Python 中的 AWS Boto 入門

上傳 HTML 表格

# Upload HTML table to S3
s3.upload_file(Filename='./jan_final_report.html', 
                      Key='2019/jan/final_report.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Python 中的 AWS Boto 入門

上傳 HTML 圖表

# Upload Aggregated Chart to S3
s3.upload_file(Filename='./jan_final_chart.html', 
                      Key='2019/jan/final_chart.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Python 中的 AWS Boto 入門

已上傳報表

已上傳報表

Python 中的 AWS Boto 入門

建立 index.html

# List the gid-reports bucket objects starting with 2019/
r = s3.list_objects(Bucket='gid-reports', Prefix='2019/')

# Convert the response contents to DataFrame objects_df = pd.DataFrame(r['Contents'])
# Create a column "Link" that contains website url + key base_url = "https://gid-reports." objects_df['Link'] = base_url + objects_df['Key']
Python 中的 AWS Boto 入門

建立 index.html

# Write DataFrame to html
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

將 DF 輸出為 html

Python 中的 AWS Boto 入門

上傳 index.html

# Upload the file to gid-reports bucket root.
s3.upload_file(
  Filename='./report_listing.html', 
  Key='index.html', 
  Bucket='gid-reports',
  ExtraArgs = {
    'ContentType': 'text/html', 
    'ACL': 'public-read'
  })
Python 中的 AWS Boto 入門

取得 index 的 URL!

Bucket 網站 URL *


"http://gid-reports.index.html"
Python 中的 AWS Boto 入門

來微調一下!

Python 中的 AWS Boto 入門

Preparing Video For Download...