Forum Archive

Uploading an image using SFTP (paramiko)

sebietter

I'm using paramiko to (successfully) connect to my server using SFTP. I tried to upload an image chosen via photos.pick_image() but can't get Pythonista to upload the image.

My code:

import paramiko
import photos
from io import BytesIO
import urllib
import datetime

# SFTP Configuration

host = 'MyHost'
port = 22
transport = paramiko.Transport((host, port))

password = 'MyPassword"
username = 'MyUsername'
transport.connect(username = username, password = password)

sftp = paramiko.SFTPClient.from_transport(transport)

# Image & Link Setup

today = datetime.datetime.now()
image = photos.pick_image()
print image
fileName = 'ios'
fileName = fileName + '_' + today.strftime("%Y-%m-%d-%H%M%S") + '.png'

urlBase = 'myURL'
encodedFileName = urllib.quote(fileName)
print fileName

remoteFilePath = 'MyPath'

# Upload the image

sftp.put(image, remoteFilePath + fileName)

sftp.close()
transport.close()
print 'Upload done!'

# Paste image link to clipboard

clipboard.set(urlBase + encodedFileName)

I'd be really grateful if one of you could explain me what I'm doing wrong and how I could solve my problem of not being able to upload an image.

briarfox

@sebietter I thought paramiko required pyCrypto which is compiled code.

ihf

I don't know that I can help with your image upload problem but I was wondering if you could say what you did to get paramiko ported to Pythonista. I tried to merely run the setup.py but got an import error relating to module osx_support.

omz

@ihf

paramiko is included in Pythonista.

briarfox

@omz you just made my day. SSH client for Shellista here we come.

ihf

import paramiko works though help(paramiko) does not. I got it to execute a remote shell cmd though other paramiko demos (e.g., demo_simple.py) will not run probably because of missing modules.

sebietter

I don't know if anybody is even interested but as a little follow up I'd like to quickly explain how I was able to solve the problem. I simply saved the image to my Pythonista library and uploaded it from there:

imgSaved = image.save(fileName)

And then upload it using the sftp.put command:

sftp.put(fileName, remoteFilePath + fileName)

With os.remove(fileName) the file can finally be removed from the Pythonista library to keep everything clean.

nasadave

Thanks for adding the follow up. I needed that.

potato

Thanks for the follow up, exactly what i needed