2015-11-30 17 views
15

Sto testando un caricamento di plupload su S3 su un server Nginx. L'intero progetto è basato su Laravel.Laravel + Caricamento caricamento di plupload alla risposta S3 per preflight non valido - CORS

Il problema è quando comincio caricati, console di Chrome dice:

XMLHttpRequest cannot load http://BUCKET.s3.amazonaws.com/. Response for preflight is invalid (redirect) 

Ho provato con le intestazioni di PHP per abilitare CORS, ma l'errore si verifica ancora.

attuale script di upload:

<?php 
$bucket = 'BUCKET'; 
$accessKeyId = '***************'; 
$secret = '********************************************'; 

$policy = base64_encode(json_encode(array(
     'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')), 
     'conditions' => array(
       array('bucket' => $bucket), 
       array('acl' => 'public-read'), 
       array('starts-with', '$key', ''), 
       array('starts-with', '$Content-Type', ''), 
       array('starts-with', '$name', ''), 
       array('starts-with', '$Filename', ''), 
     ) 
))); 

$signature = base64_encode(hash_hmac('sha1', $policy, $secret, true)); 
?> 
.... 
<div id="uploader"></div> 
....  
    <script> 
     $(function() { 
      $("#uploader").plupload({ 
       runtimes: 'html5', 
       url: 'http://<?php echo $bucket; ?>.s3.amazonaws.com/', 

       multipart: true, 
       multipart_params: { 
        'key': '${filename}', 
        'Filename': '${filename}', 
        'acl': 'public-read', 
        'Content-Type': 'image/jpeg', 
        'AWSAccessKeyId': '<?php echo $accessKeyId; ?>', 
        'policy': '<?php echo $policy; ?>', 
        'signature': '<?php echo $signature; ?>' 
       }, 

       file_data_name: 'file', 
       filters: { 
        max_file_size: '10mb', 
        prevent_duplicates: true, 
        mime_types: [ 
         {title: "Image files", extensions: "jpg,jpeg"} 
        ] 
       } 
      }); 
     }); 
    </script> 
.... 

ho controllato this per consentire CORS su Nginx, ma il fatto è quando ho messo frammento sul mio posizione/ blocco solo restituire un 404 quando ho aperto l'URL .

mio Nginx file di configurazione del sito:

server { 
    listen 80; 
    server_name myserver.com; 
    root /usr/share/nginx/myserver/public; 

    index index.html index.htm index.php; 

    charset utf-8; 

    location/{ 
     try_files $uri $uri/ /index.php?$query_string; 
    } 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location = /robots.txt { access_log off; log_not_found off; } 

    access_log off; 
    error_log /var/log/nginx/myapp-error.log error; 

    sendfile off; 

    client_max_body_size 100m; 

    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_intercept_errors off; 
     fastcgi_buffer_size 16k; 
     fastcgi_buffers 4 16k; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 

} 

Grazie mille

risposta

2

Ho trovato un modo per ottenerlo male. Studiando in profondità http://laravel.com/docs/master/filesystem non ho trovato una risposta valida per far funzionare Plupload. In caso contrario, ho trovato una soluzione povera che utilizza un:

<input type="file" name="files[]" multiple> 

e utilizzare qualcosa di simile:

foreach($request->file("files") as $file) { 
     Storage::put(
      'test'.rand(1,100), 
      file_get_contents($file->getRealPath()) 
     ); 
    }; 

Nel mio controller, che consente a più caricare file da S3. Ma non è asincrono e anche l'UX è peggio.

Continuerò a lavorare per rendere disponibile Plupload. Pubblicherò aggiornamenti qui.

Problemi correlati