Я пытаюсь использовать Cloud Storage с App Engine. Но я получаю следующую ошибку.Ошибка при использовании облачного хранилища Google
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~sigmar-notes1/1.389052547617375726/main.py", line 47, in post
real_path = os.path.join('/', bucket_name, user.user_id(), file_name)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/posixpath.py", line 75, in join
if b.startswith('/'):
AttributeError: 'NoneType' object has no attribute 'startswith'
Я новичок в App Engine, и я использую код от «Python для Google App Engine» Массимилиано Пеппи. Вот код, который я использую.
class MainHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
template_context = {}
if user is not None:
logout_url = users.create_logout_url(self.request.uri)
template_context = {
'user': user.nickname(),
'logout_url': logout_url,
}
template = jinja_env.get_template('main.html')
self.response.out.write(template.render(template_context))
else:
login_url = users.create_login_url(self.request.uri)
self.redirect(login_url)
def post(self):
user = users.get_current_user()
if user is None:
self.error(401)
bucket_name = app_identity.get_default_gcs_bucket_name()
uploaded_file = self.request.POST.get('uploaded_file')
file_name = getattr(uploaded_file, 'filename', None)
file_content = getattr(uploaded_file, 'file', None)
real_path = ''
if file_name and file_content:
content_t = mimetypes.guess_type(file_name)[0]
real_path = os.path.join('/', bucket_name, user.user_id(), file_name)
with cloudstorage.open(real_path, 'w', content_type=content_t) as f:
f.write(file_content.read())
self._create_note(user, file_name)
logut_url = users.create_logout_url(self.request.uri)
template_context = {
'user': user.nickname(),
'logout_url': logut_url,
'note_title': self.request.get('title'),
'note_content': self.request.get('content'),
}
self.response.out.write(self._render_template('main.html', template_context))
def _render_template(self, template_name, context=None):
if context is None:
context = {}
user = users.get_current_user()
ancestor_key = ndb.Key("User", user.nickname())
gry = Note.owner_query(ancestor_key)
context['notes'] = gry.fetch()
template = jinja_env.get_template(template_name)
return template.render(context)
@ndb.transactional
def _create_note(self, user, file_name):
note = Note(parent=ndb.Key("User", user.nickname()), title=self.request.get('title'), content=self.request.get('content'))
note.put()
item_titles = self.request.get('checklist_items').split(',')
for item_title in item_titles:
item = CheckListItem(parent=note.key, title=item_title)
item.put()
note.checklist_items.append(item.key)
if file_name:
note.files.append(file_name)
note.put()
Просто комментарий по вопросу, здесь слишком много кода, который действительно не имеет отношения к вопросу. Вы должны попытаться уменьшить код до минимального набора, который обнаруживает проблему. Это также поможет с вашей собственной отладкой. –