Can you upload to S3 using a stream rather than a local file?

前端 未结 5 1495
迷失自我
迷失自我 2020-11-29 23:17

I need to create a CSV and upload it to an S3 bucket. Since I\'m creating the file on the fly, it would be better if I could write it directly to S3 bucket as it is being cr

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 23:41

    There's an interesting code solution mentioned in a GitHub smart_open issue (#82) that I've been meaning to try out. Copy-pasting here for posterity... looks like boto3 is required:

    csv_data = io.BytesIO()
    writer = csv.writer(csv_data)
    writer.writerows(my_data)
    
    gz_stream = io.BytesIO()
    with gzip.GzipFile(fileobj=gz_stream, mode="w") as gz:
        gz.write(csv_data.getvalue())
    gz_stream.seek(0)
    
    s3 = boto3.client('s3')
    s3.upload_fileobj(gz_stream, bucket_name, key)
    

    This specific example is streaming to a compressed S3 key/file, but it seems like the general approach -- using the boto3 S3 client's upload_fileobj() method in conjunction with a target stream, not a file -- should work.

提交回复
热议问题