I'm downloading a zip file from S3 in Go like so:
buff := &aws.WriteAtBuffer{}
downloader := s3manager.NewDownloader(session.New(config))
_, err := downloader.Download(buff, &input)
if err != nil {
log.Println(err)
return err
}
data := buff.Bytes()
I send 'data' to a client that's written in Python3, and need to convert this byte array back to a zip file and put it in a specified directory. I've tried this:
file_bytes = msg_obj["Params"]
try:
zf = zipfile.ZipFile(file_bytes, "r")
for fileinfo in zf.infolist():
print(zf.read(fileinfo).decode('ascii'))
except: print("Err:", sys.exc_info()[0])
But I get this error:
OSError: [Errno 36] File name too long"
I just want to 'rebuild' the zip file and save it to a directory.
I figured out how to do this. In go, you'll want to base64 encode it (as a string).
buff := &aws.WriteAtBuffer{}
downloader := s3manager.NewDownloader(session.New(config))
_, err := downloader.Download(buff, &input)
if err != nil {
log.Println(err)
return err
}
data := b64.StdEncoding.EncodeToString(buff.Bytes())
Then in python it's as simple as this (where 'file_bytes' is the base64 encoded string):
d = base64.b64decode(file_bytes)
f = open('home/update_file', 'wb')
f.write(d)
f.close()
And bam, you have a reassembled zip file.
Read the documentation, the first parameter of ZipFile class is the file name or file object, not the Zip content.
If you want to read the ZipFile in memory without creating a real file, you'll want to wrap file_bytes using io.BytesIO.