How to connect to S3 in python and download a csv

我是研究僧i 提交于 2019-12-08 14:08:43

问题


I want to connect to a private s3 bucket and download a csv in python. How to do this? I see a lot of comments talking about boto3, So This is what i ve tried and it is failing.

   from boto3.session import Session
   import pandas as pd
   import boto3

   ACCESS_KEY='A'
   SECRET_KEY='s/'

   session = Session(aws_access_key_id=ACCESS_KEY,
              aws_secret_access_key=SECRET_KEY)
   s3 = session.resource('s3')

   obj = s3.get_object(Bucket='sp-dps', Key='da-la/hp/hp_co/current')

   df = pd.read_csv(obj['Body'])

回答1:


    import boto
    from boto.s3.key import Key

    keyId ="xxx" (AWS KEY)
    sKeyId="yyy" (AWS SECRET KEY ID)
    srcFileName="/abc/def/ghi/jkl/part_data_1"
    destFileName="s3_part_data_1.csv"
    bucketName="s-bucket"

    conn = boto.connect_s3(keyId,sKeyId)
    bucket = conn.get_bucket(bucketName)

    #Get the Key object of the given key, in the bucket
    k = Key(bucket,srcFileName)

    #Get the contents of the key into a file
    k.get_contents_to_filename(destFileName)

The above is the correct code.



来源:https://stackoverflow.com/questions/43806612/how-to-connect-to-s3-in-python-and-download-a-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!