Pet*_*alk 5 python google-app-engine google-cloud-storage
我找不到如何使用Python在Google App Engine上实现Google Cloud Storage Signed Urls的简单示例。请编写分步指南。:)
我创建了这个仓库:https : //github.com/voscausa/appengine-gcs-signed-url
使用GAE Pyrthonapp_identity.get_service_account_name()
和app_identity.sign_blob()
使创建已签署的网址很容易,不使用PEM关键。该应用程序展示了如何下载 GCS 文件。
但是,如果您使用 SDK 来测试应用程序,则必须使用:
因为创建签名 url 不是 GCS 客户端的一部分。
其他解决方案有效,但有一种更简单的方法使用generate_signed_url。此方法与@voscausa 的答案执行相同的操作,但不那么乏味,并且具有自定义异常并支持其他环境。
def sign_url(obj, expires_after_seconds=60):
client = storage.Client()
default_bucket = '%s.appspot.com' % app_identity.get_application_id()
bucket = client.get_bucket(default_bucket)
blob = storage.Blob(obj, bucket)
expiration_time = int(time.time() + expires_after_seconds)
url = blob.generate_signed_url(expiration_time)
return url
Run Code Online (Sandbox Code Playgroud)
vascausa 关于本地开发服务器测试的说法
但是,如果您使用 SDK 来测试应用程序,则必须使用:
--appidentity_email_address
--appidentity_private_key_path
因为创建签名 url 不是 GCS 客户端的一部分。
仍然持有。
我们是这样实现的:
\n\n第1步:获取p12文件/证书
\n\n从https://console.developers.google.com/\n\xe2\x80\x9cAPIs & auth / Credentials\xe2\x80\x9d 选项卡下载 p12 文件。
\n\n步骤2:将p12文件转换为DER格式
\n\n找到一台 Linux 计算机,使用终端打开并连接\n命令:\n openssl pkcs12 -in -nodes -nocerts > \n # p12 文件的当前 Google 密码是notasecret
命令:\n openssl rsa -in -inform PEM -out -outform DER
\n\n步骤 3:将 DER 文件转换为 base64 编码的字符串
\n\nPython 控制台:
\n\nprivate_key = open(\xe2\x80\x98<filename.der>\xe2\x80\x99, \'rb\').read()\nprint private_key.encode(\'base64\')\n
Run Code Online (Sandbox Code Playgroud)\n\n复制并粘贴到 App 引擎脚本中。
\n\n第 4 步:在 AppEngine 中启用 PyCrypto
\n\napp.yaml 必须有一行来启用 PyCrypto:
\n\n- name: pycrypto\n version: latest\n
Run Code Online (Sandbox Code Playgroud)\n\n第 5 步:创建签名 URL 的 Python 代码
\n\nimport Crypto.Hash.SHA256 as SHA256\nimport Crypto.PublicKey.RSA as RSA\nimport Crypto.Signature.PKCS1_v1_5 as PKCS1_v1_5\n\nder_key = \xe2\x80\x9c\xe2\x80\x9d\xe2\x80\x9d<copy-paste-the-base64-converted-key>\xe2\x80\x9d\xe2\x80\x9d\xe2\x80\x9d.decode(\'base64\')\n\nbucket = <your cloud storage bucket name (default is same as app id)>\nfilename = <path + filename>\n\nvalid_seconds = 5\nexpiration = int(time.time() + valid_seconds)\n\nsignature_string = \'GET\\n\\n\\n%s\\n\' % expiration\nsignature_string += bucket + filename\n\n\n\n# Sign the string with the RSA key.\nsignature = \'\'\ntry:\n start_key_time = datetime.datetime.utcnow()\n rsa_key = RSA.importKey(der_key, passphrase=\'notasecret\')\n #objects[\'rsa_key\'] = rsa_key.exportKey(\'PEM\').encode(\'base64\')\n signer = PKCS1_v1_5.new(rsa_key)\n signature_hash = SHA256.new(signature_string)\n signature_bytes = signer.sign(signature_hash)\n signature = signature_bytes.encode(\'base64\')\n\n objects[\'sig\'] = signature\nexcept:\n objects[\'PEM_error\'] = traceback.format_exc()\n\ntry:\n # Storage\n STORAGE_CLIENT_EMAIL = <Client Email from Credentials console: Service Account Email Address>\n STORAGE_API_ENDPOINT = \'https://storage.googleapis.com\'\n\n # Set the query parameters.\n query_params = {\'GoogleAccessId\': STORAGE_CLIENT_EMAIL,\n \'Expires\': str(expiration),\n \'Signature\': signature}\n\n\n # This is the signed URL:\n download_href = STORAGE_API_ENDPOINT + bucket + filename + \'?\' + urllib.urlencode(query_params)\n\nexcept:\n pass\n
Run Code Online (Sandbox Code Playgroud)\n\n来源
\n\n\n\n\n\n\n 归档时间: |
|
查看次数: |
1498 次 |
最近记录: |