带有WebPack的App Engine dev_appserver.py从Google Storage Local API返回404

时间:2017-08-26 20:28:51

标签: google-app-engine webpack

我正在使用带有Python的Google App Engine标准环境和使用Webpack的前端开发应用程序。该应用程序将照片存储在Google Storage中并显示它们。当我将应用程序部署到App Engine时,一切正常。

当我尝试运行本地dev_appserver.py和Webpack时,在通过Webpack DevServer端口(在我的情况下为3001)访问应用程序时尝试从Google Storage API读取时,我得到了例外。如果构建Webpack静态输出并使用dev_appserver.py端口(8080)执行完全相同的请求,我会将照片恢复。

[appengine] ERROR    2017-08-26 20:19:18,786 photo_storage.py:31] Fail to read photo file
[appengine] Traceback (most recent call last):
[appengine]   File "c:\myapp\server\api\photo_storage.py", line 20, in read_photo_from_storage
[appengine]     file_stat = gcs.stat(filename)
[appengine]   File "c:\myapp\server\lib\cloudstorage\cloudstorage_api.py", line 151, in stat
[appengine]     body=content)
[appengine]   File "c:\myapp\server\lib\cloudstorage\errors.py", line 132, in check_status
[appengine]     raise NotFoundError(msg)
[appengine] NotFoundError: Expect status [200] from Google Storage. But got status 404.
[appengine] Path: '/app_default_bucket/pics/185804764220139124118/395ebb68_main.png'.
[appengine] Request headers: None.
[appengine] Response headers: {'date': 'Sat, 26 Aug 2017 20:19:18 GMT', 'connection': 'keep-alive', 'content-type': 'text/html; charset=utf-8', 'content-security-policy': "default-src 'self'", 'x-content-type-options': 'nosniff', 'x-powered-by': 'Express', 'content-length': '211'}.
[appengine] Body: ''.
[appengine] Extra info: None.

这是读取和写入文件的代码:

import logging
import os
import logging
import urllib

from google.appengine.api import app_identity

import cloudstorage as gcs

from models import Photo

def read_photo_from_storage(photo, label, response):
    bucket_name = os.environ.get('BUCKET_NAME',
                                 app_identity.get_default_gcs_bucket_name())

    filename = format_photo_file_name(bucket_name, photo.created_by_user_id, photo.crc32c, label)

    try:
        file_stat = gcs.stat(filename)
        gcs_file = gcs.open(filename)
        response.headers['Content-Type'] = file_stat.content_type
        response.headers['Cache-Control'] = 'private, max-age=31536000' # cache for upto 1 year
        response.headers['ETag'] = file_stat.etag
        response.write(gcs_file.read())
        gcs_file.close()

    except gcs.NotFoundError:
        logging.exception("Fail to read photo file")
        response.status = 404
        response.write('photo file not found')

def write_photo_to_storage(user_id, checksum, label, file_type, image_content):
    bucket_name = os.environ.get('BUCKET_NAME',
                                 app_identity.get_default_gcs_bucket_name())

    filename = format_photo_file_name(bucket_name, user_id, checksum, label)

    write_retry_params = gcs.RetryParams(backoff_factor=1.1)
    gcs_file = gcs.open(filename,
                        'w',
                        content_type=file_type,
                        options={'x-goog-meta-crc32c': "{0:x}".format(checksum)},
                        retry_params=write_retry_params)
    gcs_file.write(image_content)
    gcs_file.close()

def format_photo_file_name(bucket_name, user_id, checksum, label):
    return urllib.quote("/{0}/pics/{1}/{2:x}_{3}.png".format(bucket_name, user_id, checksum, label))

0 个答案:

没有答案