简体   繁体   English

google-app-engine标准中的google-cloud-storage权限被拒绝

[英]google-cloud-storage permission denied in google-app-engine standard

Trying to access google cloud storage bucket from inside google app engine standard running flask using the client library for Python . 尝试使用Python客户端库从谷歌应用引擎标准运行烧瓶内部访问谷歌云存储桶。

( edit ) not officially supported. 编辑 )没有正式支持。 see answer for workaround. 查看解决方法的答案。

code looks something like this.. 代码看起来像这样..

from flask import Flask
from google.cloud import storage

# UNCOMMENT THIS FOR SOLUTION
#import requests_toolbelt.adapters.appengine
#requests_toolbelt.adapters.appengine.monkeypatch()

app = Flask(__name__)
@app.route('/endpoint', methods=['POST', 'PUT'])
def upload_to_storage():
    try:

      # file info
      filename = secure_filename(file.filename)
      mimetype = file.content_type

      # connect to bucket
      client = storage.Client(project='projectName')
      bucket = client.get_bucket('bucketName')

      # upload file to bucket
      blob = storage.Blob(filename, bucket)
      blob.upload_from_file(file, content_type=mimetype, num_retries=10)

      return jsonify({'status':200})

    except:

      return jsonify({'status':500})

error message: 错误信息:

('Connection aborted.', error(13, 'Permission denied')) ('连接已中止。',错误(13,'权限被拒绝'))

Answer 回答

(edit) google.cloud.storage is not officially supported in GAE standard, to get authentication to work ( google-auth ) need to do a few extra steps: (编辑)google.cloud.storage在GAE标准中不受官方支持,要使身份验证工作( google-auth )需要执行一些额外的步骤:

update requirements.txt 更新requirements.txt

requests-toolbelt 请求-工具区

assuming you are using a directory called 'lib' to vendor in 3rd party libraries 假设您在第三方库中使用名为“lib”的目录供应商

$ pip install -r requirements.txt -t lib

update appengine_config.py from google.appengine.ext import vendor vendor.add('lib') from google.appengine.ext import vendor vendor.add('lib') 更新appengine_config.py

update main.py (or equivalent) import requests_toolbelt.adapters.appengine requests_toolbelt.adapters.appengine.monkeypatch() 更新main.py(或等效的) import requests_toolbelt.adapters.appengine requests_toolbelt.adapters.appengine.monkeypatch()

update app.yaml libraries: - name: ssl version: latest 更新app.yaml libraries: - name: ssl version: latest

Instead of using the google.cloud.storage modules, use the GCS client designed specifically for operating within GAE standard: https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/setting-up-cloud-storage#downloading_the_client_library . 使用专为GAE标准操作而设计的GCS客户端,而不是使用google.cloud.storage模块: https//cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/setting-up-cloud-storage #downloading_the_client_library This client will implicitly perform the authentication avoiding that 'Permission denied' error. 此客户端将隐式执行身份验证,避免“权限被拒绝”错误。

The following page demonstrates how to write using the client: h ttps://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage#writing_to_cloud_storage . 以下页面演示了如何使用客户端进行编写:h ttps://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage#writing_to_cloud_storage The only missing information is that the "filename" in the example is in the format "//". 唯一缺少的信息是示例中的“文件名”格式为“//”。 The write operation takes care of locating and writing to the correct bucket(You do not need to retrieve the bucket beforehand). 写操作负责定位和写入正确的存储桶(您不需要事先检索存储桶)。

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

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