Åtkomst till privata objekt i S3

Introduktion till AWS Boto i Python

Maksim Pecherskiy

Data Engineer

Ladda ned en privat fil

df = pd.read_csv('https://gid-staging.potholes.csv')

Förhandsvisning

Introduktion till AWS Boto i Python

Ladda ned privata filer

Ladda ned fil

s3.download_file(
  Filename='potholes_local.csv',
  Bucket='gid-staging', 
  Key='2019/potholes_private.csv')

Läs från disk

pd.read_csv('./potholes_local.csv')
Introduktion till AWS Boto i Python

Åtkomst till privata filer

Använd '.get_object()'

obj = s3.get_object(Bucket='gid-requests', Key='2019/potholes.csv')
print(obj)
Introduktion till AWS Boto i Python

Åtkomst till privata filer

Åtkomst till privata filer

Introduktion till AWS Boto i Python

Åtkomst till privata filer

Hämta objektet

obj = s3.get_object(
  Bucket='gid-requests', 
  Key='2019/potholes.csv')

Läs StreamingBody till Pandas

pd.read_csv(obj['Body'])
Introduktion till AWS Boto i Python

Försignerade URL:er

  • Går ut efter en viss tid
  • Passar bra för tillfällig åtkomst

Exempel

https://?AWSAccessKeyId=12345&Signature=rBmnrwutb6VkJ9hE8Uub%2BBYA9mY%3D&Expires=1557624801
Introduktion till AWS Boto i Python

Försignerade URL:er

Ladda upp en fil

s3.upload_file(
  Filename='./potholes.csv', 
  Key='potholes.csv', 
  Bucket='gid-requests')
Introduktion till AWS Boto i Python

Försignerade URL:er

Generera försignerad URL

share_url = s3.generate_presigned_url(
  ClientMethod='get_object',
  ExpiresIn=3600,
  Params={'Bucket': 'gid-requests','Key': 'potholes.csv'}
)

Öppna i Pandas

pd.read_csv(share_url)
Introduktion till AWS Boto i Python

Läs in flera filer till en DataFrame

# 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/')
# Get response contents request_files = response['Contents']
Introduktion till AWS Boto i Python

Läs in flera filer till en DataFrame

# 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)
Introduktion till AWS Boto i Python

Läs in flera filer till en DataFrame

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

# Preview the DataFrame df.head()

Förhandsvisning av DataFrame

Introduktion till AWS Boto i Python

Repetition – åtkomst till privata objekt i S3

Ladda ned och öppna

s3.download_file()

Öppna direkt

s3.get_object()

Generera försignerad URL

s3.generate_presigned_url()
Introduktion till AWS Boto i Python

Repetition – dela URL:er

Offentliga filer: offentlig objekt-URL

Generera med .format()

'https://{bucket}.{key}'
Privata filer: försignerad URL

Generera med .get_presigned_url()

'https://?AWSAccessKeyId=12345&Signature=rBmnrwutb6VkJ9hE8Uub%2BBYA9mY%3D&Expires=1557624801'
Introduktion till AWS Boto i Python

Nu kör vi en övning!

Introduktion till AWS Boto i Python

Preparing Video For Download...