2024-04-18 19:20:10 发布
网友
我找不到一个简单的例子来说明如何用Python在googleappengine上实现Google云存储签名url。请写一个循序渐进的指南。:)
我创建了这个回购:https://github.com/voscausa/appengine-gcs-signed-url
使用GAE Pyrthonapp_identity.get_service_account_name()和app_identity.sign_blob()可以非常容易地创建签名的url,而无需使用PEM密钥。这个应用程序展示了如何下载GCS文件。在
app_identity.get_service_account_name()
app_identity.sign_blob()
但如果使用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
vascausa对本地开发服务器测试的看法
But if you use the SDK to test the app, you have to use: appidentity_email_address appidentity_private_key_pathbecause creating a signed url is not part of the GCS client.
But if you use the SDK to test the app, you have to use:
appidentity_email_address
appidentity_private_key_path
because creating a signed url is not part of the GCS client.
仍然有效。在
我们是如何做到的:
第1步:获取p12文件/证书
从https://console.developers.google.com/下载p12文件 “API和身份验证/凭据”选项卡。在
第2步:将p12文件转换为DER格式
找到一台打开的Linux计算机并使用终端连接 命令: openssl pkcs12-in-nodes-nocerts>; #p12文件的当前Google密码是notasecret
notasecret
命令: openssl rsa-in-inform-PEM-out-outform-DER
第3步:将DER文件转换为base64编码字符串
Python控制台:
private_key = open(‘<filename.der>’, 'rb').read() print private_key.encode('base64')
复制并粘贴到应用程序引擎脚本中。在
第4步:在AppEngine中启用PyCrypto
在应用程序yaml必须有一行才能启用PyCrypto:
第5步:创建签名URL的Python代码
import Crypto.Hash.SHA256 as SHA256 import Crypto.PublicKey.RSA as RSA import Crypto.Signature.PKCS1_v1_5 as PKCS1_v1_5 der_key = “””<copy-paste-the-base64-converted-key>”””.decode('base64') bucket = <your cloud storage bucket name (default is same as app id)> filename = <path + filename> valid_seconds = 5 expiration = int(time.time() + valid_seconds) signature_string = 'GET\n\n\n%s\n' % expiration signature_string += bucket + filename # Sign the string with the RSA key. signature = '' try: start_key_time = datetime.datetime.utcnow() rsa_key = RSA.importKey(der_key, passphrase='notasecret') #objects['rsa_key'] = rsa_key.exportKey('PEM').encode('base64') signer = PKCS1_v1_5.new(rsa_key) signature_hash = SHA256.new(signature_string) signature_bytes = signer.sign(signature_hash) signature = signature_bytes.encode('base64') objects['sig'] = signature except: objects['PEM_error'] = traceback.format_exc() try: # Storage STORAGE_CLIENT_EMAIL = <Client Email from Credentials console: Service Account Email Address> STORAGE_API_ENDPOINT = 'https://storage.googleapis.com' # Set the query parameters. query_params = {'GoogleAccessId': STORAGE_CLIENT_EMAIL, 'Expires': str(expiration), 'Signature': signature} # This is the signed URL: download_href = STORAGE_API_ENDPOINT + bucket + filename + '?' + urllib.urlencode(query_params) except: pass
来源
How to get the p12 file.
Signing instructions.
Inspiration for how to sign the url.
我创建了这个回购:https://github.com/voscausa/appengine-gcs-signed-url
使用GAE Pyrthon
app_identity.get_service_account_name()
和app_identity.sign_blob()
可以非常容易地创建签名的url,而无需使用PEM密钥。这个应用程序展示了如何下载GCS文件。在但如果使用SDK测试应用程序,则必须使用:
因为创建签名的url不是GCS客户端的一部分。在
其他解决方案也可以,但是有一种更简单的方法使用generate_signed_url。此方法的作用与@voscausa的答案相同,但不太繁琐,并且具有自定义异常和对其他环境的支持。在
vascausa对本地开发服务器测试的看法
仍然有效。在
我们是如何做到的:
第1步:获取p12文件/证书
从https://console.developers.google.com/下载p12文件 “API和身份验证/凭据”选项卡。在
第2步:将p12文件转换为DER格式
找到一台打开的Linux计算机并使用终端连接 命令: openssl pkcs12-in-nodes-nocerts>; #p12文件的当前Google密码是
notasecret
命令: openssl rsa-in-inform-PEM-out-outform-DER
第3步:将DER文件转换为base64编码字符串
Python控制台:
复制并粘贴到应用程序引擎脚本中。在
第4步:在AppEngine中启用PyCrypto
在应用程序yaml必须有一行才能启用PyCrypto:
^{pr2}$第5步:创建签名URL的Python代码
来源
How to get the p12 file.
Signing instructions.
Inspiration for how to sign the url.
相关问题 更多 >
编程相关推荐