使用python和GAE检索默认回退化身图像

2024-05-16 05:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的用户可以上传自己的图片,并将其作为头像使用。现在,如果他们自己没有上传图像,我就很难检索默认的回退图像

头像的路径是“//mysite.com/avatar/username”

到目前为止,我有这段代码,当用户自己上传了一个头像时,它可以正常工作,但是当我尝试检索默认图像时,它给了我以下错误:

raise IOError(errno.EACCES, 'file not accessible', filename)

IOError: [Errno 13] file not accessible: '/Users/myuser/Documents/github/mysite/static/images/profile.png'

def get(self):
    path = self.request.path.split('/')
    action = self.get_action(path)

    if action:
        e = employees.filter('username = ', action).get()

        if e.avatar:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(e.avatar)
        else:
            self.response.headers['Content-Type'] = 'image/png'
            path = os.path.join(os.path.split(__file__)[0], 'static/images/profile.png')
            with open(path, 'r') as f:
                print self.response.out.write(f.read())

我在app.yaml中将“/static”文件夹定义为静态目录

我知道我可以把profile.png放在根文件夹中,但我更喜欢放在“/static/images”文件夹中

有什么想法吗


Tags: path用户图像self文件夹getpngresponse
1条回答
网友
1楼 · 发布于 2024-05-16 05:18:47

如果将文件本身声明为static_file,或将其目录或其文件路径中的任何目录声明为应用程序/服务的.yaml配置文件中的static_dir,则默认情况下,应用程序代码无法访问该文件

您还需要将其配置为application_readable。从Handlers element

application_readable

Optional. Boolean. By default, files declared in static file handlers are uploaded as static data and are only served to end users. They cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.

相关问题 更多 >