2015-06-15 136 views

回答

2

下面是我們如何使它工作:

步驟1:https://console.developers.google.com/ 「的API &認證/證書」選項卡中獲取P12文件/證書

下載P12文件。

步驟2:轉換P12文件DER格式

找到一臺Linux計算機打開並使用 終端連接命令: OpenSSL的PKCS12 -in -nodes -nocerts> #目前的谷歌密碼爲P12文件notasecret

命令: OpenSSL的RSA -in -inform PEM退房手續-outform DER

第3步:轉換DER文件base64編碼字符串

的Python控制檯:

private_key = open(‘<filename.der>’, 'rb').read() 
print private_key.encode('base64') 

複製並粘貼到App Engine的腳本。

步驟4:在應用服務引擎啓用PyCrypto

app.yaml中必須有一個行來啓用PyCrypto:

- name: pycrypto 
    version: latest 

步驟5:Python代碼來創建簽名URL

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.

3

我創造了這個回購協議:https://github.com/voscausa/appengine-gcs-signed-url

使用GAE Pyrthon app_identity.get_service_account_name()app_identity.sign_blob()使創建已簽署的網址很容易,不使用PEM關鍵。該應用程序顯示如何下載GCS文件。

但是如果你使用的SDK來測試應用程序,您必須使用:

  1. --appidentity_email_address
  2. --appidentity_private_key_path

,因爲建立一個標識的URL不是一部分GCS客戶端。

0

其他解決方案的工作,但有一個更簡單的方法使用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

但是如果你使用的SDK來測試應用程序,您必須使用:

--appidentity_email_address

- appidentity_private_key_path

因爲創建簽名的url不是GCS客戶端的一部分。

仍然成立。

相關問題