通过 Python 从 AWS S3 解压缩文件



我有这段代码,可以将文件作为gzip推送到S3中。但是当将其拉回时,我不知道如何解压缩它和它并将内容存储为变量。

def push_to_s3(args,args,args):
data = response.json()
data = str(data.get("actual_data"))
inmemory = io.BytesIO() 
with gzip.GzipFile(fileobj=inmemory, mode='wb') as fh:
with io.TextIOWrapper(fh, encoding='utf-8',errors='replace') as wrapper:
wrapper.write(json.dumps(data, ensure_ascii=False,indent=2))
inmemory.seek(0)  
cv.s3_resource.Object(args, args + ) + '.json.gz').upload_fileobj(inmemory)     
inmemory.close()

下面是我尝试拉下文件、读取内容并将其存储为值以供以后使用的地方。

obj = s3client.get_object(Bucket = Bucket, Key = key)
content = obj['Body'].read()
print(content) 

我只需要将内容翻译回文本并将其分配给一个变量,但我无法弄清楚如何(它只是一个 9 位数字(

>>> with gzip.open(obj['Body']) as f:
content = f.read().decode('utf-8')
>>> content
'"186686935"'

最新更新