Reputation: 43
I'm running a Flask webserver on an EC2 Ubuntu instance. The purpose is to capture the body of an incoming POST, write it to a file locally, then upload that file to S3.
The code is, basically:
@app.route('/', methods=['GET','POST'])
@app.route('/index.html', methods=['GET','POST'])
def index_home():
data = request.data
with open('test.json', 'w') as f:
f.write(data)
## Upload the stuff to S3
When I run it on the local Flask webserver instance and send a POST with a json body from Postman, it works perfectly. But on the EC2 instance I'm getting a permissions error (according to the apache error.log) on the 'test.json' file, which results in 500 error on page-load.
I've scoured Google and Stackoverflow (Here is a similar question, with no solution) to no avail. This seems like an easy problem, but I've tried everything and can't seem to get it to work: I've added my user to the www-data group, I've changed the /var/www folders and files permissions to every combination of root, ubuntu (the default EC2 Ubuntu user) and www-data I could think of, I've straight 777 the directories...nothing seems to work.
Obviously, I'm a bit of a newbie. Is there a configuration file or something that requires a tweak to get this to work?
Upvotes: 1
Views: 5955
Reputation: 653
You should make sure the program is actually trying to write in the directory you want it to write to. It could be that it tries to write into the directory of the Python binary (or anything else), that depends on your command and the current working directory. For testing purposes you might try to change the path like so (make sure /tmp is writeable for the user, which should be the case):
with open('/tmp/test.json', 'w') as f:
f.write(data)
Upvotes: 5