PUT gzip directly to S3 in Python

Thing you want to do

Sample code

lambda_function.py


import csv
import gzip
import io
import os
import boto3
import cx_Oracle

DB_HOST = os.environ.get('DB_HOST')
DB_PORT = os.environ.get('DB_PORT')
DB_USER = os.environ.get('DB_USER')
DB_PASS = os.environ.get('DB_PASS')
DB_SID = os.environ.get('DB_SID')

AWS_S3_BUCKET_NAME = 'hoge'

s3 = boto3.client('s3')


def lambda_handler(event, context):

    file_name = 'hoge.csv.gz'
    #oracle connection
    connection = cx_Oracle.connect(DB_USER, DB_PASS, f'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={DB_HOST}) (PORT={DB_PORT}))(CONNECT_DATA=(SID={DB_SID})))')
    with connection:
        with connection.cursor() as cursor:
            sql = 'SELECT * FROM hoge'
            cursor.execute(sql)
            #Table column name
            csv_header = [column_name[0] for column_name in cursor.description]

            bio = io.BytesIO()
            with gzip.GzipFile(fileobj=bio, mode='wb') as gzip_file:
                with io.TextIOWrapper(gzip_file, encoding='utf-8') as wrapper:
                    writer = csv.writer(wrapper, lineterminator='\n', quoting=csv.QUOTE_ALL)
                    writer.writerow(csv_header)
                    while 1:
                        csv_detail = cursor.fetchmany()
                        if len(csv_detail) == 0:
                            break
                        writer.writerows(csv_detail)
            bio.seek(0)
            s3.put_object(Bucket=AWS_S3_BUCKET_NAME, Body=bio, Key=file_name)

    return event

Summary

At first, I created a file in tmp and then uploaded it to S3. I was able to PUT directly to S3 by using io.BytesIO.

Recommended Posts

PUT gzip directly to S3 in Python
3 steps to put Python + mecab in yum only
To add a module to python put in Julialang
Sample to put Python Kivy in one file
To flush stdout in Python
Login to website in Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Solve island hunting (equivalent to paiza rank S) in Python
Solve mod7 fortune-telling (equivalent to paiza rank S) in Python
Convert markdown to PDF in Python
Put MeCab in "Windows 10; Python3.5 (64bit)"
How to collect images in Python
Put python, numpy, opencv3 in ubuntu14
How to use SQLite in Python
Until you put Python in Docker
In the python command python points to python3.8
Try to calculate Trace in Python
Try to put data in MongoDB
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
How to use python put in pyenv on macOS with PyCall
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python
How to do R chartr () in Python
Send email to multiple recipients in Python (Python 3)
Convert psd file to png in Python
Sample script to trap signals in Python
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
Log in to Slack using requests in Python
How to get a stacktrace in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
[Python] Pandas to fully understand in 10 minutes
Throw Incoming Webhooks to Mattermost in Python
Module to generate word N-gram in Python
To reference environment variables in Python in Blender
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
[V11 ~] A memorandum to put in Misskey
How to dynamically zero pad in Python