การแชร์ไฟล์ผ่านเว็บไซต์

Python เบื้องต้นสำหรับ AWS Boto

Maksim Pecherskiy

Data Engineer

การเสิร์ฟหน้า HTML

S3 serving HTML

Python เบื้องต้นสำหรับ AWS Boto

ตาราง HTML ใน Pandas

แปลง DataFrame เป็น HTML

df.to_html('table_agg.html')

DataFrame to HTML

Python เบื้องต้นสำหรับ AWS Boto

ตาราง HTML ใน Pandas พร้อมลิงก์

แปลง DataFrame เป็น HTML

df.to_html('table_agg.html', render_links=True)

DataFrame to HTML

Python เบื้องต้นสำหรับ AWS Boto

เลือกบางคอลัมน์เป็น HTML

แปลง DataFrame เป็น HTML

df.to_html('table_agg.html', 
           render_links=True,
           columns['service_name', 'request_count', 'info_link'])

DataFrame to HTML

Python เบื้องต้นสำหรับ AWS Boto

เส้นขอบ

แปลง DataFrame เป็น HTML

df.to_html('table_agg.html', 
           render_links=True,
           columns['service_name', 'request_count', 'info_link'],
           border=0)

DataFrame to HTML borders

Python เบื้องต้นสำหรับ AWS Boto

การอัปโหลดไฟล์ HTML ขึ้น S3

อัปโหลดไฟล์ HTML ขึ้น S3

s3.upload_file(
  Filename='./table_agg.html', 
  Bucket='datacamp-website',

Key='table.html',
ExtraArgs = { 'ContentType': 'text/html', 'ACL': 'public-read'} )
Python เบื้องต้นสำหรับ AWS Boto

การเข้าถึงไฟล์ HTML

รูปแบบ URL ของ S3 Object

https://{bucket}.{key}
https://datacamp-website.table.html
Python เบื้องต้นสำหรับ AWS Boto

หน้า HTML

หน้า HTML

Python เบื้องต้นสำหรับ AWS Boto

การอัปโหลดไฟล์ประเภทอื่น

อัปโหลดไฟล์รูปภาพขึ้น S3

s3.upload_file(
  Filename='./plot_image.png', 
  Bucket='datacamp-website',
  Key='plot_image.png',

ExtraArgs = { 'ContentType': 'image/png', 'ACL': 'public-read'} )
Python เบื้องต้นสำหรับ AWS Boto

IANA Media Types

  • JSON : application/json
  • PNG : image/png
  • PDF : application/pdf
  • CSV : text/csv
1 http://www.iana.org/assignments/media-types/media-types.xhtml
Python เบื้องต้นสำหรับ AWS Boto

การสร้างหน้าดัชนี

# 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'])

หน้าดัชนี

Python เบื้องต้นสำหรับ AWS Boto

การสร้างหน้าดัชนี

# Create a column "Link" that contains website url + key
base_url = "http://datacamp-website."
objects_df['Link'] = base_url + objects_df['Key']
# Write DataFrame to html
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

การสร้างหน้าดัชนี

Python เบื้องต้นสำหรับ AWS Boto

การอัปโหลดหน้าดัชนี

อัปโหลดไฟล์ HTML ขึ้น S3

s3.upload_file(
  Filename='./report_listing.html', 
  Bucket='datacamp-website',
  Key='index.html',
  ExtraArgs = {
    'ContentType': 'text/html', 
    'ACL': 'public-read'}
)

https://datacamp-website.index.html

Python เบื้องต้นสำหรับ AWS Boto

ทบทวน

  • ตาราง HTML ใน Pandas (df.to_html('table.html'))
  • อัปโหลดไฟล์ HTML (ContentType: text/html)
  • อัปโหลดไฟล์รูปภาพ (ContentType: image/png)
  • แชร์ URL ของหน้า HTML!
Python เบื้องต้นสำหรับ AWS Boto

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับ AWS Boto

Preparing Video For Download...