I want to zip a string(could be very big) and send it through FTP. So far I am using ftplib and ziplib, but they are not getting along too well.
ftp = FTP(self.host)
ftp.login(user=self.username, passwd=self.password)
ftp.cwd(self.remote_path)
buf = io.BytesIO(str.encode("This string could be huge!!"))
zip = ZipFile.ZipFile(buf, mode='x')
# Either one of the two lines
ftp.storbinary("STOR " + self.filename, buf) # Works perfectly!
ftp.storbinary("STOR " + self.filename, zip) # Doesnt Work
ftp.quit()
The line that doesn't work throws me the following error.
KeyError: 'There is no item named 8192 in the archive'
I tried to zip the file to bytesio without success.
I need to do this all in memory. I cant write the zip file on the server first and then ftp.
Also, I need to do it through pure FTP, no SFTP nor SSH.
I think you're taking the problem the wrong way round.
ftp.storbinary
needs abytes
object, not aZipFile
object. You need to createbytes
object with compressed data out of your uncompressed data, and pass that toftp.storbinary
. Plus you have to provide a name for the file in the archive.this snippet creates such an object from a string (standalone example)
now adapted to your context:
The
seek
part is needed else you're passing theoutput_io
file-like object while at the end of the file (you just wrote to it so current position is: end of stream). Usingseek(0)
rewinds the file-like object so it can be read from the start.note that for only one file, it may be better to use a
Gzip
object.