2016-10-12 16 views
1

私は約1日でこれに苦労しています。 Azure Blobストレージのアップロードを直接テストし、恐ろしいCORSの問題を取得しています。私はすでに試してみました"XMLHttpRequest cannot load https://tempodevelop.blob.core.windows.net/tmp/a4d8e867-f13e-343f-c6d3-a603…Ym0PlrBn%2BU/UzUs7QUhQw%3D&sv=2014-02-14&se=2016-10-12T17%3A59%3A26.638531. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 403."Azure Blobへのアップロード中に、要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。

もの:

  1. は、すべてのホストにCORSを設定します。 enter image description here
  2. は、ローカルに私のアプリをホスティングしていると私は使用してファイルをアップロードすることができることを確認しました
  3. Herokuの上で試してみました別のツール(Azureストレージエクスプローラ)
  4. は私のAccessPolicyを 'rwdl'に設定しましたが、アクセス署名(ユニットテストで検証済み)が確実に得られました。 https://github.com/mikebz/azureupload

    しかし、関連部分は、ここでフロントエンドのアップロードである:

全体としてのコードは、ここに提供されてPythonで

<script> 
 

 
    /* 
 
    * not a true GUID, see here: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript 
 
    */ 
 
    function guid() { 
 
     function s4() { 
 
      return Math.floor((1 + Math.random()) * 0x10000) 
 
      .toString(16) 
 
      .substring(1); 
 
     } 
 
     return s4() + s4() + '-' + s4() + '-' + s4() + '-' + 
 
      s4() + '-' + s4() + s4() + s4(); 
 
    } 
 

 
    function startUpload() { 
 
     var fileName = guid(); 
 

 
     jQuery.getJSON("/formfileupload/signature/" + fileName , function(data) { 
 
       console.log("got a signature: " + data.bloburl); 
 
       uploadFile(data.bloburl, data.signature); 
 
      }) 
 
      .fail(function(jqxhr, textStatus, error) { 
 
       console.log("error: " + textStatus + " - " + error); 
 
      }) 
 
    } 
 
    
 
    function uploadFile(bloburl, signature) {   
 
     var xhr = new XMLHttpRequest(); 
 
     fileData = document.getElementById('fileToUpload').files[0]; 
 
     xhr.open("PUT", bloburl + "?" + signature); 
 
     xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob'); 
 
     xhr.setRequestHeader('x-ms-blob-content-type', fileData.type); 
 
     result = xhr.send(fileData); 
 
    } 
 
</script>

署名生成コードここにある:

def generate_access_signature(self, filename): 
    """ 
    calls the Azure Web service to generate a temporary access signature. 
    """ 
    blob_service = BlobService(
     account_name=self.account_name, 
     account_key=self.account_key 
    ) 

    expire_at = datetime.utcnow() 
    expire_at = expire_at + timedelta(seconds = 30) 
    access_policy = AccessPolicy(permission="rwdl", expiry=expire_at.isoformat()) 

    sas_token = blob_service.generate_shared_access_signature( 
     container_name="tmp", 
     blob_name = filename, 
     shared_access_policy=SharedAccessPolicy(access_policy) 
    ) 
    return sas_token 

答えて

3

[応答にHTTPステータスコード403がありました]というエラーメッセージによると、CORSがサービスに対して有効になっていないか、プリフライトリクエストに一致するCORSルールがない可能性があります。詳細Cross-Origin Resource Sharing (CORS) Support for the Azure Storage Servicesを参照してください。 または、SAS署名が正しくない可能性があります。 は、ブロブサービスの下Azureのポータルに設定CORSをチェックする

  1. 試しのトラブルシューティングをしようとしていてください。テーブル、キュー、ファイルのような他のサービスがあるので。あなたはSASトークン enter image description here

はSASを取得し、生成されたSAS demo code enter image description here

+0

感謝トム!私はあなたの歩みに従った。手動で生成された署名の場合でも、結果は同じです。おそらくこのシステムが新しいデータをアップロードするのに役立たないかもしれないバグがあるのだろうか?わからない... – mikebz

+0

私の経験では、システムとの関係はありません。 Azure PortalのBlobサービスのCROS設定をもう一度確認していますか? ブロブのURLと署名をあなたの生成したものと置き換え、もう一度スクリプトを試してください。 そして、ChromeやEdgeのようなブラウザでデバッグしようとします。 解決できない場合は、CROS設定を提供して、私のようなトラブルシューティングスナップショットを閲覧してもかまいませんか? –

+0

CORSをすべての原点で使用できるようにするには安全ですか? –

1

でそれをデバッグしよう生成するのに使用することができます Expected

  • またAzure exploreツールトムのおかげでマイクロソフトのサポートは、この問題が解決されました。
    ソリューションパート#1 - Pythonバージョン0.33以降用のAzureストレージライブラリを使用していることを確認してください。

    azure-common==1.1.4 
    azure-nspkg==1.0.0 
    azure-storage==0.33.0 
    cffi==1.8.3 
    cryptography==1.5.2 
    dj-database-url==0.4.1 
    Django==1.10.2 
    enum34==1.1.6 
    futures==3.0.5 
    gunicorn==19.6.0 
    idna==2.1 
    ipaddress==1.0.17 
    pep8==1.7.0 
    psycopg2==2.6.2 
    pyasn1==0.1.9 
    pycparser==2.16 
    python-dateutil==2.5.3 
    requests==2.11.1 
    six==1.10.0 
    whitenoise==3.2.2 
    

    第二の問題は、署名を生成している。ここで

    は私の要件ファイルです。右の署名を生成するコードはここにある:

    from azure.storage.blob import BlockBlobService, ContainerPermissions 
    from datetime import datetime, timedelta 
    
    
    class AzureUtils: 
    
        def __init__(self, account_name, account_key): 
         if account_name is None: 
          raise ValueError("account_name should not be None") 
         if account_key is None: 
          raise ValueError("account_key should not be None") 
         self.account_name = account_name 
         self.account_key = account_key 
    
        def generate_access_signature(self, filename): 
         """ 
         calls the Azure Web service to generate a temporary access signature. 
         """ 
         block_blob_service = BlockBlobService(
          account_name=self.account_name, 
          account_key=self.account_key 
         ) 
    
         expire_at = datetime.utcnow() 
         expire_at = expire_at + timedelta(seconds=30) 
    
         permissions = ContainerPermissions.READ | ContainerPermissions.WRITE | ContainerPermissions.DELETE | ContainerPermissions.LIST 
    
         sas_token = block_blob_service.generate_container_shared_access_signature(
           "tmp", 
           permission=permissions, 
           expiry=expire_at 
         ) 
    
         return sas_token 
    

    ソリューションはまた、ここで取得することができます:https://github.com/mikebz/azureupload

  • 関連する問題