【问题标题】:How to connect to S3 in python and download a csv如何在 python 中连接到 S3 并下载 csv
【发布时间】:2023-03-19 19:07:01
【问题描述】:

我想连接到一个私有 s3 存储桶并在 python 中下载一个 csv。 这个怎么做?我看到很多 cmets 都在谈论 boto3,所以这是我尝试过的,但它失败了。

   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'])

【问题讨论】:

  • 请添加异常/错误信息。

标签: python csv dataframe amazon-s3 boto


【解决方案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)

以上是正确的代码。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2020-05-25
    • 2010-10-07
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多