Python เบื้องต้นสำหรับ AWS Boto
Maksim Pecherskiy
Data Engineer
df = pd.read_csv('https://gid-staging.potholes.csv')

ดาวน์โหลดไฟล์
s3.download_file(
Filename='potholes_local.csv',
Bucket='gid-staging',
Key='2019/potholes_private.csv')
อ่านจากดิสก์
pd.read_csv('./potholes_local.csv')
ใช้ '.get_object()'
obj = s3.get_object(Bucket='gid-requests', Key='2019/potholes.csv')
print(obj)

ดึงออบเจกต์
obj = s3.get_object(
Bucket='gid-requests',
Key='2019/potholes.csv')
อ่าน StreamingBody เข้า Pandas
pd.read_csv(obj['Body'])
ตัวอย่าง
https://?AWSAccessKeyId=12345&Signature=rBmnrwutb6VkJ9hE8Uub%2BBYA9mY%3D&Expires=1557624801
อัปโหลดไฟล์
s3.upload_file(
Filename='./potholes.csv',
Key='potholes.csv',
Bucket='gid-requests')
สร้าง Presigned URL
share_url = s3.generate_presigned_url(
ClientMethod='get_object',
ExpiresIn=3600,
Params={'Bucket': 'gid-requests','Key': 'potholes.csv'}
)
เปิดใน Pandas
pd.read_csv(share_url)
# 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']
# 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)
# Concatenate all the DataFrames in the list df = pd.concat(df_list)# Preview the DataFrame df.head()

ดาวน์โหลดแล้วเปิด
s3.download_file()
เปิดโดยตรง
s3.get_object()
สร้าง presigned URL
s3.generate_presigned_url()
สร้างด้วย .format()
'https://{bucket}.{key}'
สร้างด้วย .get_presigned_url()
'https://?AWSAccessKeyId=12345&Signature=rBmnrwutb6VkJ9hE8Uub%2BBYA9mY%3D&Expires=1557624801'
Python เบื้องต้นสำหรับ AWS Boto