1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python3】S3のファイルをメモリ上でZIP化しSFTPでファイルを転送する。

Last updated at Posted at 2023-02-10
import os
import traceback
from io import BytesIO
import zipfile
import socket

import paramiko
import boto3

AWS_PROFILE = 'XXXXXXXX'

host = 'XXX.XXX.X.X'
port = 22
username = 'XXXXXX'
sshkey_path = 'XXXXX.pem'


def s3_object_to_zip_obj(bucket, key):
    session = boto3.Session(profile_name=AWS_PROFILE)
    s3 = session.resource('s3')
    s3_bucket = s3.Bucket(bucket)
    filecols = s3_bucket.objects.filter(Prefix=key).all()

    archive = BytesIO()

    with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as zip_archive:
        for s3_obj in filecols:
            with zip_archive.open(s3_obj.key, 'w') as zip_f:
                zip_f.write(s3_obj.get()['Body'].read())

    archive.seek(0)
    return archive



if __name__ == '__main__':
    try:
        host_key = paramiko.RSAKey.from_private_key_file(sshkey_path)
        sshcon = paramiko.SSHClient()
        sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        sshcon.connect(host, port, username, pkey=host_key)
        sftp = sshcon.open_sftp()

        bucket = 'XXXX'
        key = 'XXXX/XXX.txt'
        zip_obj = s3_object_to_zip_obj(bucket, key)

        sftp_to = 'XXXX/XXXX.zip'
        sftp.putfo(zip_obj, sftp_to)
        zip_obj.close()

        sftp.close()
        sshcon.close()
    except:
        print('# error')
        print(traceback.format_exc())
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?