我无法将文件名中有空格的文件上传到谷歌云端存储,我可以上传没有空格的文件名
这是我的PHP代码
set_include_path("../google-api-php-client-master/src/" . PATH_SEPARATOR . get_include_path());
require_once "Google/Signer/P12.php";
require_once "Google/Utils.php";
/**
* Create a signed URL to access the named resource at the cloud
* storage facility. Right now, this is Google Cloud Storage. The
* URL will be returned to the client, which uses Cross-Origin Resource
* Sharing (CORS) to access the file directly.
*/
function storageURL($fileName, $method = 'GET', $content_type = "", $duration = 10 ) {
$expires = time( ) + $duration*60;
$to_sign = ($method . "\n" .
/* Content-MD5 */ "\n" .
$content_type . "\n" .
$expires . "\n" .
'/bucketname/' . $fileName);
$signature = '*Signature will go here*';
$signer = new Google_Signer_P12(file_get_contents(__DIR__.'/'."final.p12"), "notasecret");
$signature = $signer->sign($to_sign);
//$signature = Google_Utils::urlSafeB64Encode($signature); // This doesn't work somehow
$signature = urlencode(base64_encode($signature));
return ('https://storage.googleapis.com/bucketname/' . $fileName .
'?GoogleAccessId=' . 'serviceaccount@developer.gserviceaccount.com' .
'&Expires=' . $expires . '&Signature=' . $signature);
}
if(!empty($_POST["name"])){
echo storageURL($_POST["name"], "PUT", $_POST["type"]);
}
这是HTML / Javascript代码
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<script src="jquery-1.11.1.js"></script>
<script>
$(document).ready(function() {
// Change handler
$('#fileSelection').change(function(event){
var file = event.target.files[0];
var data = {'name' : file.name, 'type' : file.type}
$.ajax({
type: "POST",
url: "signurl.php",
data: data,
success: function(result, success, response) {
console.log(arguments);
$('#sendButton').attr('disabled', false);
$('#sendButton').attr('uploadURL', result);
}
});
});
// Click handler
$('#sendButton').click(function(){
$('#sendButton').attr('disabled', true);
$('#fileSelection').prop('files')[0];
var file = $('#fileSelection').prop('files')[0]; // Assume a single file selection
if (!file) return; // If no file, do nothing
var xhr = new XMLHttpRequest(); // Create a new request
var uploadURL = $('#sendButton').attr('uploadURL');
xhr.open("PUT", uploadURL);
$('#sendButton').attr('uploadURL', "");
xhr.setRequestHeader("Content-Length", file.size);
xhr.setRequestHeader("Content-Type", file.type);
xhr.onreadystatechange = function(){
if (xhr.readyState === 4) {// request is done
if (xhr.status === 200) {// successfully
location.reload();
}
}
};
xhr.send(file);
});
})
</script>
</head>
<body>
Upload
<br>
<input id="fileSelection" type="file" name="uploaded_files" size="40">
<input id="sendButton" type="submit" value="Send" disabled>
</body>
</html>
我也讨论了类似问题here。
我应该怎样做才能实现这一目标?
答案 0 :(得分:0)
function storageURL($fileName, $method = 'GET', $content_type = "", $duration = 10 ) {
$expires = time( ) + $duration*60;
$to_sign = ($method . "\n" .
/* Content-MD5 */ "\n" .
$content_type . "\n" .
$expires . "\n" .
'/bucketname/' . $fileName);
$signature = '*Signature will go here*';
$signer = new Google_Signer_P12(file_get_contents(__DIR__.'/'."final.p12"), "notasecret");
$signature = $signer->sign($to_sign);
$signature = rawurlencode(base64_encode($signature));
return ('https://storage.googleapis.com/bucketname/' . $fileName .
'?GoogleAccessId=' . 'serviceaccount@developer.gserviceaccount.com' .
'&Expires=' . $expires . '&Signature=' . $signature);
}