Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从s3存储桶读取gzip文件_Python_Amazon Web Services_Amazon S3_Gzip_Boto3 - Fatal编程技术网

Python 从s3存储桶读取gzip文件

Python 从s3存储桶读取gzip文件,python,amazon-web-services,amazon-s3,gzip,boto3,Python,Amazon Web Services,Amazon S3,Gzip,Boto3,嘿,我正在尝试从s3 bucket读取gzip文件,下面是我的尝试: s3client = boto3.client( 's3', region_name='us-east-1' ) bucketname = 'wind-obj' file_to_read = '20190101_0000.gz' fileobj = s3client.get_object( Bucket=bucketname, Key=file_to_read ) filedata

嘿,我正在尝试从s3 bucket读取gzip文件,下面是我的尝试:

s3client = boto3.client(
    's3',
    region_name='us-east-1'
)

bucketname = 'wind-obj'
file_to_read = '20190101_0000.gz'

fileobj = s3client.get_object(
    Bucket=bucketname,
    Key=file_to_read
    )

filedata = fileobj['Body'].read()
现在打开gzip文件,我的做法如下:

gzip.open(filedata,'rb')
但这让我犯了一个错误:

ValueError: embedded null byte
所以我首先尝试解码它:

contents = filedata.decode('utf-8')
这会引发另一个错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
我曾尝试使用
ISO-8859-1
对其进行解码,但在打开gzip文件时再次出现相同的错误


或者有没有其他方法可以像使用URL之类从S3中提取数据?

gzip.open
需要一个文件名或一个已经打开的文件对象,但您直接将下载的数据传递给它。尝试使用
gzip。请解压缩

filedata = fileobj['Body'].read()
uncompressed = gzip.decompress(filedata)

您使用的是什么版本的Python?@AnonCoward Python 3.8.6是的,我知道了,非常感谢!