Php 从AWS S3上传获取json
我正在结合使用将文件上载到S3。我的目标是获取AWS存储的文件名。如果我上传foo.mov十次,S3会将其命名为foo.mov、foo(1).mov、foo(2).mov等等。成功上传后,我在控制台中收到来自以下方法的JSON字符串响应:Php 从AWS S3上传获取json,php,jquery,amazon-s3,jquery-file-upload,Php,Jquery,Amazon S3,Jquery File Upload,我正在结合使用将文件上载到S3。我的目标是获取AWS存储的文件名。如果我上传foo.mov十次,S3会将其命名为foo.mov、foo(1).mov、foo(2).mov等等。成功上传后,我在控制台中收到来自以下方法的JSON字符串响应: protected function generate_response($content, $print_response = true) { if ($print_response) { $json = json_encode($c
protected function generate_response($content, $print_response = true) {
if ($print_response) {
$json = json_encode($content);
$redirect = isset($_REQUEST['redirect']) ?
stripslashes($_REQUEST['redirect']) : null;
if ($redirect) {
$this->header('Location: '.sprintf($redirect, rawurlencode($json)));
return;
}
$this->head();
if ($this->get_server_var('HTTP_CONTENT_RANGE')) {
$files = isset($content[$this->options['param_name']]) ?
$content[$this->options['param_name']] : null;
if ($files && is_array($files) && is_object($files[0]) && $files[0]->size) {
$this->header('Range: 0-'.(
$this->fix_integer_overflow(intval($files[0]->size)) - 1
));
}
}
$this->body($json);
}
return $content;
}
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = $this->get_server_var('HTTP_CONTENT_DISPOSITION') ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$this->get_server_var('HTTP_CONTENT_DISPOSITION')
)) : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
$size = $content_range ? $content_range[3] : null;
$files = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$files[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
if( isset($_POST['fileSourceChooser']) && $_POST['fileSourceChooser']=='dropbox' ){
//http://justinvincent.com/page/1087/how-to-get-the-mime-type-of-a-remote-file-in-php-with-redirects
function get_url_mime_type($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
}
$upload=$_POST['files']; ////if just a regular post
$upload['parse_url']=parse_url($upload['link']);
$upload['url']='https://dl.dropboxusercontent.com'.$upload['parse_url']['path'];
$files[] = $this->handle_file_upload(
$upload['url'],
$file_name ? $file_name : $upload['name'],
$size ? $size : $upload['bytes'],
get_url_mime_type($upload['url']),
"",
null, null,
$content_range
);
file_put_contents(
$this->options['upload_dir'].'thumbnail/'.$upload['name'],
fopen($upload['thumbnail'], 'r'),
FILE_APPEND //$append_file ? FILE_APPEND : 0
);
}
else{
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$files[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $this->get_server_var('CONTENT_LENGTH')),
isset($upload['type']) ?
$upload['type'] : $this->get_server_var('CONTENT_TYPE'),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
}
$AWSresponse = $this->generate_response(
array($this->options['param_name'] => $files),
$print_response
);
return $AWSresponse;
}
该响应如下所示:
{"files":[{"name":"foo (51).mov","size":1202952,"type":"video\/quicktime","url":"https:\/\/me.s3.amazonaws.com\/bucket\/foo%20%2851%29.mov","deleteUrl":"https:\/\/myurl.com\/folder\/path\/?file=foo%20%2851%29.mov&_method=DELETE","deleteType":"POST"}]}
如何从UploadHandler类外部访问此信息?我想我可以尝试在post方法(名为AWSresponse)中设置一个变量,将其设置为这个值,但这不起作用
该类通过以下方式执行:
require('UploadHandler.php');
$upload_handler = new UploadHandler();
我希望能够这样做(这不起作用):
如果我上传foo.mov十次,S3将命名为。。。S3不命名(或重命名)任何东西。S3中对象的名称是代码发送的名称。True。除非文件名与bucket中的另一个文件名匹配。不,这是不正确的。如果名称与另一个对象匹配,S3没有行为不同的概念。在这种情况下,旧对象只是被覆盖。(如果启用了bucket版本控制,则旧版本不会被销毁,但它只能通过不透明的版本标识符访问,即使这样名称也不会更改。)如果文件的名称不同,S3绝对不会这样做。。。无论你用什么代码上传文件,都必须这样做。我坚持更正!我以为是S3——我错过了重写。但是第二个问题仍然代表着从uploadhandler中获取名称:当类通过
$upload\u handler::$AWSresponse
显示空值时,如何访问类中的变量,但是如果我在方法中回显$AWSresponse变量,它就有内容了?
require('UploadHandler.php');
$upload_handler = new UploadHandler();
$response = $upload_handler::$AWSresponse;