Buzz
Buzz

Reputation: 516

Problems uploading PiCamera images directly to FTP server

I have a Raspberry Pi camera which I would like to use to capture images and store them directly to an FTP server. I would like to bypass having to store the images on the SD Card because the camera will be used in a remote environment with little maintenance, hence I would like to avoid potential SD Card failures by writing directly to my FTP.

I have the following script:

import ftplib
import time
import picamera

with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
for filename in camera.capture_continuous('img{counter:03d}.jpg'):
    print('Captured %s' % filename)

    server = 'server.address'
    username = 'user'
    password = 'pass'
    ftp_connection = ftplib.FTP(server, username, password)
    remote_path = "/Cam/"
    ftp_connection.cwd(remote_path)
    fh = open("/home/pi" + filename, 'rb')
    ftp_connection.storbinary('STOR ', fh)
    fh.close()
    time.sleep(60) # wait 1 minute

Instead of uploading the images to the FTP, my script is saving the images to the home directory. Infact, it is saving an image every second.

How can I solve this problem?

Upvotes: 1

Views: 702

Answers (1)

dbmitch
dbmitch

Reputation: 5386

You're not specifying the output filename

Replace:

ftp_connection.storbinary('STOR ', fh)

With:

ftp_connection.storbinary('STOR ' + filename, fh)

Upvotes: 2

Related Questions