简体   繁体   English

获取Google App Engine平台中服务器中文件夹中的静态文件列表

[英]Get list of static files in a folder in server in Google App Engine platform

I am using Google App Engine for hosting my website which is mostly static pages. 我使用Google App Engine来托管我的网站,这主要是静态页面。 But there is a part where I need to get a list of images from a folder in the server and dynamically add those images to the page using JavaScript. 但是有一部分我需要从服务器中的文件夹中获取图像列表,并使用JavaScript将这些图像动态添加到页面中。

I got it working in my local machine using os.walk("static/images/temp/1.jpg") in a python script. 我在python脚本中使用os.walk("static/images/temp/1.jpg")在我的本地机器上工作。 But after deploying the site, it doesn't work. 但在部署网站后,它不起作用。 The python script return empty. python脚本返回空。 Should there be a different approach for getting list of static files in Google App Engine? 是否有不同的方法来获取Google App Engine中的静态文件列表?

The main issue is that files in a static directory (which I'm assuming you're using to serve your files) are stored in a different location than your code, and are therefore not in your code's file directory. 主要问题是静态目录中的文件(我假设您用来提供文件)存储在与代码不同的位置,因此不在代码的文件目录中。

One thing that you can do (see here for a similar issue) is create symlinks to your files (or just copy them) in another directory that you do not declare as a static_dir (or at all) in your app.yaml . 您可以做的一件事(请参阅此处查找类似问题)是在您的app.yaml 未将其声明为static_dir (或根本没有 )的另一个目录中创建文件的符号链接(或仅复制它们)。 It will then live with your application code and you can reference it using os . 然后它将与您的应用程序代码一起使用,您可以使用os引用它。 For instance, here is some very simple code that iterates through the files in a directory called upload_stuff in the root: 例如,这里有一些非常简单的代码,它遍历根目录中名为upload_stuff的目录中的文件:

class MainPage(webapp2.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'upload_stuff')
    for filename in os.listdir(path):
      # Here you can do what you need with the files
      self.response.out.write(filename)

You can adjust this to work how you want, but the basic idea is that you will need to store the files with your code as well as in static form. 您可以根据需要调整此项,但基本思路是您需要使用代码以及静态格式存储文件。

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

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