[英]Upload files to Google Cloud Storage Bucket using PHP
I've searched a lot (and even found some helpful links like this one Upload a file to Google Cloud Storage Bucket with PHP and Ajax ) but I couldn't get it working. 我搜索了很多(甚至找到了一些有用的链接,比如这个用PHP和Ajax将文件上传到谷歌云存储桶 ),但我无法让它工作。 I need to upload a txt file to my google cloud storage bucket. 我需要将txt文件上传到我的Google云存储分区。
Here's my code: 这是我的代码:
gcp_storage.php: gcp_storage.php:
<?php
if(isset($_FILES['file'])){
$errors= array();
// Authentication with Google Cloud Platform
$client = new StorageClient(['keyFilePath' => 'cloudtest-2412**-a66e94ba56**.json']);
$bucket = $client->bucket('cloudtest-2412**.appspot.com ');
// Upload a file to the bucket.
$file = pathinfo($_FILES['file']['name']);
$prefix = uniqid('prefix_', true); //generate random string to avoid name conflicts
$filename = $prefix . "." . $file['extension'];
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r'),
['name' => $filename]
);
}
?>
html and js: html和js:
<html>
<form id="fileForm" action="" method="post" enctype="multipart/form-data">
<div class="form-row">
<br/>
<div class="form-group">
<label for="textFile">Text File: </label>
<br/>
<input type="file" name="file" id="textFile">
</div>
</div>
<br>
<button type="submit" class="btn btn-lg btn-primary"> Upload </button>
</form>
</html>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
$(document).on('submit', '#fileForm', function (e) {
e.preventDefault();
var data = new FormData($('#fileForm').get(0));
$.ajax({
type: 'POST',
url: 'gcp_storage.php',
data: data,
processData: false,
contentType: false,
success: function ($data) {
console.log('Succcess');
$('#success').attr('hidden', false);
}
});
});
</script>
*It shows on my console success but when I go to my bucket on google cloud, nothing appears... *它显示在我的控制台上成功但是当我在谷歌云上找到我的桶时,什么都没有出现......
*I downloaded the .json file from the IAM page (the file is in the same directory as the other filse) *我从IAM页面下载了.json文件(该文件与其他文件位于同一目录中)
Don't know if this really helps, if I remove the use Google\\Cloud\\Storage\\StorageClient
, PHP throws me the error Class 'StorageClient' not found in /base/data/home/apps/i
(when I put back the use code it just throws the 500 error, see below) 不知道这是否真的有帮助,如果我删除use Google\\Cloud\\Storage\\StorageClient
,PHP会抛出错误Class 'StorageClient' not found in /base/data/home/apps/i
(当我放回使用代码它只会抛出500错误,见下文)
I've been changing the code and putting breakpoints to see if I find where the 500 error is coming from. 我一直在改变代码并设置断点,看看我是否找到了500错误的来源。 If I remove everything from my PHP file and just put echo "Test"
, it works fine. 如果我从我的PHP文件中删除所有内容并将echo "Test"
,它可以正常工作。
After putting some breakpoints at the original code, I found out that the 500 error comes from this lines of code: 在原始代码上放置一些断点后,我发现500错误来自这行代码:
$client = new StorageClient(['keyFilePath' => 'cloudtest-2412**-a66e94ba56**.json']);
$bucket = $client->bucket('cloudtest-2412**.appspot.com');
(The authentication code) (验证码)
Any suggestions? 有什么建议么?
Thanks 谢谢
I think, we can see a permission error. 我想,我们可以看到权限错误。 In most cases, an internal 500 server error is due to incorrect permission in one or more files or folders. 在大多数情况下,内部500服务器错误是由于一个或多个文件或文件夹中的错误权限造成的。
Please try with this steps: https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application 请尝试执行以下步骤: https : //cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.