get data from s3 bucket python

Solutions on MaxInterview for get data from s3 bucket python by the best coders in the world

showing results for - "get data from s3 bucket python"
Alexa
12 Feb 2020
1#Get data from s3 bucket python:
2
3def s3_read(source, profile_name=None):
4    """
5    Read a file from an S3 source.
6
7    Parameters
8    ----------
9    source : str
10        Path starting with s3://, e.g. 's3://bucket-name/key/foo.bar'
11    profile_name : str, optional
12        AWS profile
13
14    Returns
15    -------
16    content : bytes
17
18    botocore.exceptions.NoCredentialsError
19        Botocore is not able to find your credentials. Either specify
20        profile_name or add the environment variables AWS_ACCESS_KEY_ID,
21        AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
22        See https://boto3.readthedocs.io/en/latest/guide/configuration.html
23    """
24    session = boto3.Session(profile_name=profile_name)
25    s3 = session.client('s3')
26    bucket_name, key = mpu.aws._s3_path_split(source)
27    s3_object = s3.get_object(Bucket=bucket_name, Key=key)
28    body = s3_object['Body']
29    return body.read()