Я не могу загрузить файл с пробелом в имени файла в облачное хранилище Google, я могу загрузить имена файлов без пробелов
Вот мой 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=' . '[email protected]' .
'&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>
Я также обсуждал подобную проблему Вот.
Что я должен сделать, чтобы достичь этой цели?
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=' . '[email protected]' .
'&Expires=' . $expires . '&Signature=' . $signature);
}
Других решений пока нет …