![](/img/trans.png)
[英]Upload a file to Google Cloud Storage Bucket with PHP and Ajax
[英]PHP, ajax, HTML form to upload file to google cloud storage
我已经看到一堆问题在问同样的事情,但我无法弄清楚这里的问题是什么。 我正在尝试通过 PHP、ajax 和 HTML 将文件上传到 Google Cloud Storage:
PHP文件
<?php
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
if(isset($_FILES['file'])){
$errors= array();
// Authentication with Google Cloud Platform
$client = new StorageClient(['keyFilePath' => 'qpack-325-13-9bc27ee4320d.json']);
$bucket = $client->bucket('qpack-13-img');
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r')
);
}
?>
jQuery/Ajax:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).on('submit', '#imageUpload', function (e) {
e.preventDefault();
var data = new FormData($('#imageUpload').get(0));
$.ajax({
type: 'POST',
url: 'config.php',
data: data,
processData: false,
contentType: false,
success: function ($data) {
console.log('Succcess');
$('#success').attr('hidden', false);
}
});
});
</script>
和 HTML:
<form id="imageUpload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Use this photo</button>
</form>
但它不会工作! 它不会抛出任何错误,ajax 调用记录“成功”,但没有任何内容上传到存储桶。 我给了 allUsers 存储对象查看器权限,我正在部署中测试它,但仍然没有。 我是否遗漏了什么,因为我似乎无法弄清楚。
我想也许它与 app.yaml 有关系,所以我把它包括在这里:
runtime: php
env: flex
runtime_config:
document_root: .
# [START gae_flex_storage_yaml]
env_variables:
GOOGLE_STORAGE_BUCKET: "***"
# [END gae_flex_storage_yaml]
您提到您已授予Storage Object Viewer
权限,但这些还不足以在您的存储上创建文件,如果您查看对象权限文档,您可以检查是否应该使用storage.objects.create
来创建文件,以及根据应用程序的需要考虑给予storage.objects.update
和storage.objects.delete
权限可能会很有趣。
注意:后台可能发生错误并以某种方式被捕获或处理,但无论如何都会向您的 AJAX 调用抛出成功状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.