2015-11-30 22 views
15

Tôi đang thử tải lên plupload lên S3 trên máy chủ Nginx. Toàn bộ dự án dựa trên Laravel.Laravel + Tải lên Plupload lên phản hồi của S3 cho preflight không hợp lệ - CORS

Vấn đề là khi tôi bắt đầu tải lên, giao diện điều khiển của Chrome nói:

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

Tôi đã cố gắng sử dụng các header PHP để cho phép CORS, nhưng lỗi vẫn xảy ra.

Current tải lên kịch bản:

<?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> 
.... 

Tôi đã kiểm tra this để cho phép CORS trên Nginx, nhưng thực tế là khi tôi đưa đoạn mã đó vào địa điểm/ khối của tôi nó chỉ trả lại 404 khi tôi mở URL .

My Nginx tập tin cấu hình trang web:

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; 
    } 

} 

Cảm ơn bạn rất nhiều

Trả lời

2

Tôi đã tìm ra cách để đạt được kết quả kém. Nghiên cứu sâu hơn http://laravel.com/docs/master/filesystem Tôi không tìm thấy câu trả lời hợp lệ để Plupload hoạt động. Nếu không, tôi tìm thấy một giải pháp kém đó là sử dụng một:

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

Và sử dụng một cái gì đó như:

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

Trong điều khiển của tôi, cho phép nhiều tập tin tải lên S3. Nhưng nó không phải là không đồng bộ và nó cũng là UX là tồi tệ hơn.

Tôi sẽ tiếp tục làm việc để kích hoạt Plupload cuối cùng. Tôi sẽ đăng thông tin cập nhật tại đây.

Các vấn đề liên quan