无法在GAE应用中使用google-cloud

nef*_*urk 6 python google-app-engine google-cloud-storage google-cloud-platform

我的Google App Engine应用(webapp.py)中的以下行无法导入Google Cloud库:

from google.cloud import storage
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

ImportError: No module named google.cloud.storage
Run Code Online (Sandbox Code Playgroud)

我做了一些研究,发现以下文章很有帮助:

结合上述文章提出的技术,我做了以下工作:

  1. 创建一个requirements.txt文件:

    google-cloud==0.19.0
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用pip以下方法导入此库

    pip install -t lib -r requirements.txt
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在我的appengine_config.py文件中使用以下代码:

    import os
    import sys
    import google
    libDir = os.path.join(os.path.dirname(__file__), "lib")
    google.__path__.append(os.path.join(libDir, "google"))
    sys.path.insert(0, libDir)
    
    Run Code Online (Sandbox Code Playgroud)

任何人都可以阐明我可能缺少的东西让这个工作吗?我只是想编写一个可以从Google云存储写入/读取的Google App Engine应用程序,我想在部署之前在本地进行测试.

man*_*nRo 5

看起来唯一需要的是包含google-cloud在项目requirements.txt文件中.

检查这个简单的示例是否适合您(您不应该得到任何导入错误).创建以下文件并运行pip install -r requirements.txt -t lib.我的网站上不再需要它来使其工作.

的app.yaml

application: mysample
runtime: python27
api_version: 1
threadsafe: true

handlers:
  - url: /.*
    script: main.app
Run Code Online (Sandbox Code Playgroud)

main.py

import webapp2
from google.cloud import storage


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
Run Code Online (Sandbox Code Playgroud)

appengine_config.py

from google.appengine.ext import vendor
import os

# Third-party libraries are stored in "lib", vendoring will make
# sure that they are importable by the application.
if os.path.isdir(os.path.join(os.getcwd(), 'lib')):
    vendor.add('lib')
Run Code Online (Sandbox Code Playgroud)

requirements.txt

google-cloud
Run Code Online (Sandbox Code Playgroud)


Bre*_*ttJ 3

App Engine SDK附带了一个特定于 App Engine 的 Google Cloud Storage API,您可以使用它来处理 Cloud Storage 存储桶。

import cloudstorage as gcs
Run Code Online (Sandbox Code Playgroud)

您是否有理由不使用这个不需要配置即可加载的内置库?