2016-06-11 40 views
24

Sto provando a scaricare file da Firebase Storage tramite XMLHttpRequest, ma Access-Control-Allow-Origin non è impostato sulla risorsa, quindi non è possibile. C'è un modo per impostare questa intestazione sul server di archiviazione?Firebase Storage and Access-Control-Allow-Origin

(let [xhr (js/XMLHttpRequest.)] 
    (.open xhr "GET" url) 
    (aset xhr "responseType" "arraybuffer") 
    (aset xhr "onload" #(js/console.log "bin" (.-response xhr))) 
    (.send xhr))) 

Chrome messaggio di errore:

XMLHttpRequest cannot load https://firebasestorage.googleapis.com/[EDITED] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3449 ' is therefore not allowed access.

+1

Non abbastanza rep per un commento, ma il modo di cui sopra è ancora il modo giusto. Volevo solo condividere i documenti ufficiali di Firebase su questo: https://firebase.google.com/docs/storage/web/download-files#cors_configuration –

risposta

48

Da questo post on the firebase-talk group/list:

The easiest way to configure your data for CORS is with the gsutil command line tool. The installation instructions for gsutil are available at https://cloud.google.com/storage/docs/gsutil_install . Once you've installed gsutil and authenticated with it, you can use it to configure CORS.

For example, if you just want to allow object downloads from your custom domain, put this data in a file named cors.json (replacing "https://example.com" with your domain):

[ 
    { 
    "origin": ["https://example.com"], 
    "method": ["GET"], 
    "maxAgeSeconds": 3600 
    } 
] 

Then, run this command (replacing "exampleproject.appspot.com" with the name of your bucket):

gsutil cors set cors.json gs://exampleproject.appspot.com 

and you should be set.

If you need a more complicated CORS configuration, check out the docs at https://cloud.google.com/storage/docs/cross-origin#Configuring-CORS-on-a-Bucket .

+2

C'è un modo per consentire tutte le origini simili a Access-Control-Allow- Origine: * ? – user1311069

+2

Come posso trovare il nome esatto del mio secchio Firebase? – Jim

+0

Dove posso trovare il cors.json? – isuru