【问题标题】:Read csv from Amazon s3 using python2.7使用 python2.7 从 Amazon s3 读取 csv
【发布时间】:2017-09-06 20:08:54
【问题描述】:

我可以轻松地从 s3 获取存储桶名称,但是当我从 s3 读取 csv 文件时,每次都会出错。

import boto3
import pandas as pd

s3 = boto3.client('s3',
         aws_access_key_id='yyyyyyyy',
         aws_secret_access_key='xxxxxxxxxxx')
# Call S3 to list current buckets
response = s3.list_buckets()
for bucket in response['Buckets']:
    print bucket['Name']

output
s3-bucket-data

.

import pandas as pd
import StringIO
from boto.s3.connection import S3Connection

AWS_KEY = 'yyyyyyyyyy'
AWS_SECRET = 'xxxxxxxxxx'
aws_connection = S3Connection(AWS_KEY, AWS_SECRET)
bucket = aws_connection.get_bucket('s3-bucket-data')

fileName = "data.csv"

content = bucket.get_key(fileName).get_contents_as_string()
reader = pd.read_csv(StringIO.StringIO(content))

出现错误-

boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request

如何从 s3 读取 csv?

【问题讨论】:

标签: python python-2.7 csv pandas amazon-s3


【解决方案1】:

你可以使用s3fs

s3fs 还支持凭证文件中的 aws 配置文件。

这是一个例子(你不必分块,但我只是方便地使用这个例子),

import os
import pandas as pd
import s3fs
import gzip

chunksize = 999999
usecols = ["Col1", "Col2"]

filename = 'some_csv_file.csv.gz'
s3_bucket_name = 'some_bucket_name'

AWS_KEY = 'yyyyyyyyyy'
AWS_SECRET = 'xxxxxxxxxx'
s3f = s3fs.S3FileSystem(
    anon=False,
    key=AWS_KEY,
    secret=AWS_SECRET)

# or if you have a profile defined in credentials file:
#aws_shared_credentials_file = 'path/to/aws/credentials/file/'
#os.environ['AWS_SHARED_CREDENTIALS_FILE'] = aws_shared_credentials_file
#s3f = s3fs.S3FileSystem(
#    anon=False,
#    profile_name=s3_profile)

filepath = os.path.join(s3_bucket_name, filename)
with s3f.open(filepath, 'rb') as f:
    gz = gzip.GzipFile(fileobj=f)  # Decompress data with gzip

    chunks = pd.read_csv(gz,
                            usecols=usecols,
                            chunksize=chunksize,
                            iterator=True,
                            )

    df = pd.concat([c for c in chunks], axis=1)

【讨论】:

    【解决方案2】:

    boto 是我在使用 python 处理 S3 上的数据时喜欢的东西。

    使用pip install boto安装boto

    import boto
    from boto.s3.key import Key
    
    keyId ="your_aws_key_id"
    sKeyId="your_aws_secret_key_id"
    srcFileName="abc.txt" # filename on S3
    destFileName="s3_abc.txt" # output file name
    bucketName="mybucket001" # S3 bucket name 
    
    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)
    

    【讨论】:

      【解决方案3】:

      我在几个 AWS 区域遇到了这个问题。我在“us-east-1”中创建了一个存储桶,以下代码运行良好:

      import boto
      from boto.s3.key import Key
      import StringIO
      import pandas as pd
      keyId ="xxxxxxxxxxxxxxxxxx"
      sKeyId="yyyyyyyyyyyyyyyyyy"
      srcFileName="zzzzz.csv"
      bucketName="elasticbeanstalk-us-east-1-aaaaaaaaaaaa"
      
      conn = boto.connect_s3(keyId,sKeyId)
      bucket = conn.get_bucket(bucketName)
      k = Key(bucket,srcFileName)
      content = k.get_contents_as_string()
      reader = pd.read_csv(StringIO.StringIO(content))
      

      尝试在 us-east-1 中创建一个新存储桶,看看是否可行。

      【讨论】:

        【解决方案4】:

        尝试以下方法:

        import boto3
        from boto3 import session
        import pandas as pd
        import io
        
        session = boto3.session.Session(region_name='XXXX')
        s3client = session.client('s3', config = 
        boto3.session.Config(signature_version='XXXX'))
        response = s3client.get_object(Bucket='myBucket', Key='myKey')
        
        dataset = pd.read_csv(io.BytesIO(response['Body'].read()), encoding='utf8')
        

        【讨论】:

        • 我得到的回复是加密的。如何解密和读取数据。
        • 您能说明一下您所说的加密是什么意思吗? (例如,分享您收到的回复)。您尝试从 S3 读取的文件是否已加密?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-14
        • 2015-12-04
        • 2015-08-04
        • 1970-01-01
        相关资源
        最近更新 更多