使用Paramiko将文件从AWS S3 bucket复制到SFTP

2024-09-26 22:55:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将所有CSV文件从S3存储桶复制到SFTP位置

我能够连接并建立连接,但我不确定如何使用Paramiko移动文件

以下代码:

print('Establishing SFTP connection with ' + hostname + ' at port: ' + port)
filebjects = s3_client.list_objects_v2(Bucket=bucket_name)
# print(filebjects)

try:
    print("port")
    # return

    bucket = s3_rs.Bucket(str(keybucket))
    for obj in bucket.objects.all():
        key = obj.key
        body = obj.get()['Body'].read()
        decodedKey = body.decode('utf-8')

    key = paramiko.RSAKey.from_private_key(StringIO(decodedKey))
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    transport = paramiko.Transport((hostname,int(port)))
    transport.connect(username=username,pkey=key)
    sftp = paramiko.SFTPClient.from_transport(transport)

    print("sftp")
    client.put(localpath="fileName",remotepath="SFTP_REMO_FOLDER",)

    sftp.chdir(path=remote_directory)

    print(sftp.listdir(path='.'))

    # for object in filebjects['Contents']:
    #     obj=client.get_object(Bucket=bucket_name,Key=fileName)
    #     file_data = obj['Body'].read()
    #     print(file_data)


    # return
    transport.close()

我不确定这里的文件名应该是什么,我将所有对象和文件名分开。有什么建议吗


Tags: 文件keyclientobjparamikos3bucketport

热门问题