Compartir archivos a través de un sitio web

Introducción a AWS Boto en Python

Maksim Pecherskiy

Data Engineer

Servir páginas HTML

S3 sirviendo HTML

Introducción a AWS Boto en Python

Tabla HTML en pandas

Convertir DataFrame a HTML

df.to_html('table_agg.html')

DataFrame a HTML

Introducción a AWS Boto en Python

Tabla HTML en pandas con enlaces

Convertir DataFrame a HTML

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

DataFrame a HTML

Introducción a AWS Boto en Python

Columnas específicas a HTML

Convertir DataFrame a HTML

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

DataFrame a HTML

Introducción a AWS Boto en Python

Bordes

Convertir DataFrame a HTML

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

DataFrame a HTML con bordes

Introducción a AWS Boto en Python

Subir un archivo HTML a S3

Subir un archivo HTML a S3

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

Key='table.html',
ExtraArgs = { 'ContentType': 'text/html', 'ACL': 'public-read'} )
Introducción a AWS Boto en Python

Acceder al archivo HTML

Plantilla de URL de objeto S3

https://{bucket}.{key}
https://datacamp-website.table.html
Introducción a AWS Boto en Python

Página HTML

Página HTML

Introducción a AWS Boto en Python

Subir otros tipos de contenido

Subir una imagen a S3

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

ExtraArgs = { 'ContentType': 'image/png', 'ACL': 'public-read'} )
Introducción a AWS Boto en Python

Tipos multimedia de IANA

  • JSON : application/json
  • PNG : image/png
  • PDF : application/pdf
  • CSV : text/csv
1 http://www.iana.org/assignments/media-types/media-types.xhtml
Introducción a AWS Boto en Python

Generar una página de índice

# Lista los objetos del bucket gid-reports que empiezan por 2019/
r = s3.list_objects(Bucket='gid-reports', Prefix='2019/')

# Convierte el contenido de la respuesta en DataFrame objects_df = pd.DataFrame(r['Contents'])

Página de índice

Introducción a AWS Boto en Python

Generar una página de índice

# Crea una columna "Link" con la URL del sitio + key
base_url = "http://datacamp-website."
objects_df['Link'] = base_url + objects_df['Key']
# Escribe el DataFrame a HTML
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

Generar una página de índice

Introducción a AWS Boto en Python

Subir la página de índice

Subir un archivo HTML a 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

Introducción a AWS Boto en Python

Resumen

  • Tabla HTML en pandas (df.to_html('table.html'))
  • Sube el archivo HTML (ContentType: text/html)
  • Sube la imagen (ContentType: image/png)
  • Comparte la URL de tu página HTML
Introducción a AWS Boto en Python

¡Vamos a practicar!

Introducción a AWS Boto en Python

Preparing Video For Download...