updated writeS3 to check for folder before write and moved all into S3 module