简体   繁体   English

使用Google云端存储时出错

[英]Error using Google Cloud Storage

I'm trying to use Cloud Storage with App Engine. 我正在尝试将Cloud Storage与App Engine一起使用。 But I get the following error. 但是我收到以下错误。

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'

I'm new to App Engine and I'm using code from "Python for google App Engine" by Massimiliano Pippi. 我是App Engine的新手,正在使用Massimiliano Pippi的“ Python for google App Engine”中的代码。 Here is the code I'm using. 这是我正在使用的代码。

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()

您对os.path.join调用的参数之一是None

real_path = os.path.join('/', bucket_name, user.user_id(), file_name)

It turned out that the default bucket had not been created. 原来,尚未创建默认存储桶。 I had to use the old app engine console to create it. 我必须使用旧的App Engine控制台来创建它。 Here is a link to the solution 这是解决方案的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM