vpe*_*osa 15 php nginx amazon-s3 plupload laravel
我在Nginx服务器上测试一个上传到S3的plupload.整个项目以Laravel为基础.
问题是当我开始上传时,Chrome的控制台说:
XMLHttpRequest cannot load http://BUCKET.s3.amazonaws.com/. Response for preflight is invalid (redirect)
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用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>
....
Run Code Online (Sandbox Code Playgroud)
我检查了这个以在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;
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢
我找到了一种方法来实现它,效果很差。深入研究http://laravel.com/docs/master/filesystem我没有找到让 Plupload 工作的有效答案。否则,我发现一个糟糕的解决方案使用:
<input type="file" name="files[]" multiple>
Run Code Online (Sandbox Code Playgroud)
并使用类似的东西:
foreach($request->file("files") as $file) {
Storage::put(
'test'.rand(1,100),
file_get_contents($file->getRealPath())
);
};
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,可以将多个文件上传到 S3。但它不是异步的,而且用户体验也更差。
我将继续努力最终启用 Plupload。我将在这里发布更新。
| 归档时间: |
|
| 查看次数: |
840 次 |
| 最近记录: |