What are you attempting to do when you encounter this error? This is a simple permissions error, make sure the apache/nginx user who is attempting to write to the directory has write access.
I ran into this same exact error when generating an xml file to the pub/ directory, and all the permissions seemed correct. Why couldn't it write the sitemap to the pub/ directory? I had access! After some digging, the error was being triggered when adding the sitemap to the robots.txt at the root, and I did not have write access to the root. Keep this in mind when troubleshooting permissions error, you may have to dig a bit based on what you are doing.