Trying to access google cloud storage bucket from inside google app engine standard running flask using the client library for Python .
(edit) not officially supported. see answer for workaround.
code looks something like this..
from flask import Flask
from google.cloud import storage
# UNCOMMENT THIS FOR SOLUTION
#import requests_toolbelt.adapters.appengine
#requests_toolbelt.adapters.appengine.monkeypatch()
app = Flask(__name__)
@app.route('/endpoint', methods=['POST', 'PUT'])
def upload_to_storage():
try:
# file info
filename = secure_filename(file.filename)
mimetype = file.content_type
# connect to bucket
client = storage.Client(project='projectName')
bucket = client.get_bucket('bucketName')
# upload file to bucket
blob = storage.Blob(filename, bucket)
blob.upload_from_file(file, content_type=mimetype, num_retries=10)
return jsonify({'status':200})
except:
return jsonify({'status':500})
error message:
('Connection aborted.', error(13, 'Permission denied'))