使用 PHP 和 Ajax 将文件上传到 Google Cloud Storage Bucket
我正在处理一个 PHP 项目,在该项目中我需要将一个文件(由 HTML 表单提供)上传到 Google Cloud Storage Bucket.表单请求将作为 ajax 请求接收.我的文件上传成功,但是在存储桶中,它显示了一个临时文件,但我已经上传了一个图像文件.
这是我尝试过的:
PHP 文件:
if(isset($_FILES['file'])){$错误=数组();//使用谷歌云平台进行身份验证$client = new StorageClient(['keyFilePath' => 'api-project-374381085870-eaf930d9ffd7.json']);$bucket = $client->bucket('storage_client');//将文件上传到存储桶.$bucket->上传(fopen($_FILES['file']['name'], 'r'));}
Ajax 代码:
$(document).on('submit', '#fileForm', function (e) {e.preventDefault();var data = new FormData($('#fileForm').get(0));$.ajax({类型:'POST',网址:'gcp_storage.php',数据:数据,处理数据:假,内容类型:假,成功:函数($data){console.log('成功');$('#success').attr('hidden', false);}});});
HTML 代码:
<form id="fileForm" action="" method="post" enctype="multipart/form-data"><div class="form-row"><br/><div class="form-group"><label for="textFile">文本文件:</label><br/><输入类型="文件" 名称="文件" id="文本文件"></div></div><br><button type="submit" class="btn btn-lg btn-primary">上传</按钮></表格>
<块引用>
图像文件在存储桶中的外观如下:
更新:
现在,我变了:
这个:
//上传文件到bucket.$bucket->上传(fopen($_FILES['file']['name'], 'r'));
收件人:
//上传文件到bucket.$bucket->上传(fopen($_FILES['file']['tmp_name'], 'r'));
文件上传成功,但文件扩展名不显示在存储桶中.那么,有没有办法在上传到谷歌云存储桶之前重命名文件?
请帮帮我!
提前致谢!
解决方案添加带有名称的 $options 参数解决问题.
$bucket->上传(fopen($_FILES['file']['tmp_name'], 'r'),['名称' =>'一些名字.jpg']);
更多信息可以在源代码中找到:https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/Storage/src/Bucket.php
更多信息:
$bucket->upload 的结果是一个StorageObject.您可以通过以下方式获取签名 URL:
$obj = $bucket->upload(...);$url = $obj->signedUrl(new Timestamp(new DateTime('tomorrow')));
更多信息可以在 https://github.com/GoogleCloudPlatform/google-cloud-php/tree/master/Storage/src.
签名 URL 的信息:https://cloud.google.com/storage/docs/access-control/signed-urls
I'm working on a PHP project in which I need to upload a file (provided by an HTML form) to the Google Cloud Storage Bucket. The form request will be received as an ajax request. My file is uploading successfully but inside the bucket, it shows a temp file but I have uploaded an image file.
Here's what I have tried:
PHP File:
if(isset($_FILES['file'])){
$errors= array();
// Authentication with Google Cloud Platform
$client = new StorageClient(['keyFilePath' => 'api-project-374381085870-eaf930d9ffd7.json']);
$bucket = $client->bucket('storage_client');
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['name'], 'r')
);
}
Ajax Code:
$(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);
}
});
});
HTML Code:
<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>
Here's how an image file looks inside bucket:
Update:
Now, I have changed:
this:
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['name'], 'r')
);
to:
// Upload a file to the bucket.
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r')
);
It's uploading the files successfully but the file extension doesn't display inside the bucket. So, is there a way I can rename the file before uploading to google cloud storage bucket?
Help me, please!
Thanks in advance!
解决方案add $options params with name solve the problem.
$bucket->upload(
fopen($_FILES['file']['tmp_name'], 'r'),
['name' => 'some-name.jpg']
);
More info can be found in the source code: https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/Storage/src/Bucket.php
More information:
the result of $bucket->upload is an StorageObject. You can get the signed URL through:
$obj = $bucket->upload(...);
$url = $obj->signedUrl(new Timestamp(new DateTime('tomorrow')));
More info can be found in https://github.com/GoogleCloudPlatform/google-cloud-php/tree/master/Storage/src.
information of signed URLS: https://cloud.google.com/storage/docs/access-control/signed-urls
相关文章