我在Nginx服务器上测试一个上传到S3的plupload。 整个项目以Laravel为基础。
问题是,当我开始上传时,Chrome控制台会说:
XMLHttpRequest cannot load http://BUCKET.s3.amazonaws.com/. Response for preflight is invalid (redirect)
我已尝试使用PHP标头启用CORS,但仍会出现错误。
当前上传脚本:
<?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>
....
我检查了this以在Nginx上启用CORS,但事实是当我将该代码段放在我的位置/ 块上时,它会在我打开URL时返回404。
我的Nginx站点配置文件:
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;
}
}
非常感谢
答案 0 :(得分:2)
我找到了一种方法来实现它。深入研究http://laravel.com/docs/master/filesystem我没有找到一个有效的答案来让Plupload工作。否则,我找到了一个使用:
的糟糕解决方案<input type="file" name="files[]" multiple>
使用类似的东西:
foreach($request->file("files") as $file) {
Storage::put(
'test'.rand(1,100),
file_get_contents($file->getRealPath())
);
};
在我的控制器中,可以将多个文件上传到S3。但它不是异步的,而且它的用户体验更糟糕。
我会继续努力最终启用Plupload。我会在这里发布更新。
答案 1 :(得分:1)