如何在GAE(python)中通过app.yaml上传静态文件?

3 投票
3 回答
2102 浏览
提问于 2025-04-17 01:28

我正在用谷歌应用引擎(GAE)做一个项目,但遇到了一个大麻烦。

我想做一个推特机器人,所以我开始的第一步就是发布推文。我在和'dailybasic.py'同一个文件夹里创建了一个'tweets.txt'文件。

这里是一些代码的片段。

#app.yaml

application: mathgirlna
version: 1
runtime: python
api_version: 1

handlers:
# - url: /static
#  static_dir: static

- url: /dailybasic   
  script: dailybasic/dailybasic.py 

- url: /.*
  script: main.py

main.py(这个可以正常工作,没有错误)

#!/usr/bin/python  
# -*- coding: utf-8 -*-

import os
import sys

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import wsgiref.handlers

class MainPage(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, None))


application = webapp.WSGIApplication([('/', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

dailybasic.py(每5分钟运行一次)

#!/usr/bin/python  
# -*- coding: utf-8 -*-

import os
import sys
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
import tweepy
import wsgiref.handlers
import time

def tweetit(tweet):
   if len(tweet)<140:
      api.update_status(tweet)
   else:
      diaryentries.append(tweet)

consumer_key = '******************'
consumer_secret = '*******************************************'
access_token = '**************************************************'
access_token_secret = '****************************************'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

class dailybasic(webapp.RequestHandler):
    def get(self):
        now = time.localtime()
        path = os.path.join(os.path.dirname(__file__), 'tweets.txt')
        f_db = open(path, 'r')
        db = f_db.readline() 
        while db != '':
            todaynow = []
            wday = now.tm_wday
            if db[(wday+1)%7]=='1' and now.tm_hour * 60 + now.tm_min <= int(db[8:10]) * 60 + int(db[11:13]) and now.tm_hour * 60 + now.tm_min + 5 > int(db[8:10]) * 60 + int(db[11:13]) :
                todaynow.append(db[14:])
        if(len(todaynow) != 0):
            import random
            tweetit(todaynow[random.randrange(0,len(todaynow)-1)])


application = webapp.WSGIApplication([('/dailybasic', dailybasic)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

cron.yaml

cron:
- description: day process
  url: /dailybasic
  schedule: every 5 minutes from 06:00 to 01:30
  timezone: Asia/Seoul

我在网上查了这个问题,尝试了所有可以放在'app.yaml'中'##'部分的东西,但都没用(虽然可以部署,但GAE警告说'处理程序引用的文件未找到:dailybasic.py')。

这是我的文件结构:

  • 根目录
    • dailybasic
      • dailybasic.py
      • tweets.txt
    • main.py
    • app.yaml, cron.yaml, index.yaml
    • index.html

我想让'index.html'只包含HTML代码,不带任何脚本。

我应该如何放置这些文件,并编写app.yaml呢?

(抱歉我的英语不好)

*补充说明

问题是,open()函数无法工作,因为'tweets.txt'没有上传或者放在错误的目录里。

3 个回答

0

为什么不把文件上传到主目录,然后直接用:

open("tweets.txt") 

而不指定路径呢?

我在GAE上用这个方法读取.csv文件没有问题。

7

静态文件只能直接通过在app.yaml中指定的URL提供给用户。你的应用程序无法读取这些文件,因为它们被部署到只提供静态文件的服务器上,而不是运行你应用程序的基础设施上。

如果你只需要从脚本中读取这些文件,可以把它们上传为非静态文件。如果你需要同时将文件静态地提供给用户的浏览器,并且还想从脚本中读取它们,那么你需要在你的应用程序中包含这两个文件的副本(不过在非静态目录中的符号链接会算作第二个副本并被部署)。

1

路径是相对于包含 app.yaml 文件的目录来指定的,所以你可以试试这样:

handlers: 
- url: /dailybasic   
  script: dailybasic/dailybasic.py 

你是想把文件 index.html 映射到根网址 / 吗?应用引擎不像其他一些网络服务器那样自动处理这个映射。要做到这个映射,可以试试下面的方式:

- url: /
  static_files: index.html
  upload: index.html

撰写回答