我正在尝试使用谷歌云存储 JSON API 将图像上传到谷歌云存储桶。文件正在上传,但没有显示任何内容。
<?php
if(isset($_POST["submit"]))
{
// Move uploaded file to a temp location
$uploadDir = 'temp/';
$filename=$_FILES["fileToUpload"]["name"];
$filesize=$_FILES["fileToUpload"]["size"];
$uploadFile = $uploadDir . basename($_FILES['fileToUpload']['name']);
$authheaders = array(
"Authorization: Bearer xxxxxx(my access token)",
"Content-Type: image/jpeg",
"Content-Length:".$filesize
); //The access-token has to be generate everytime after one-hour.
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadFile))
{
// Prepare remote upload data
$uploadRequest = array(
'fileName' => basename($uploadFile),
'fileData' => file_get_contents($uploadFile)
);
// Execute remote upload
$curl = curl_init();
$url="https://www.googleapis.com/upload/storage/v1/b/[bucket_name]/o?uploadType=media&name=".$filename;
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $authheaders);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadRequest);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}
?>
我正在通过这个上传图片:-
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
查看图像编号 1,文件已成功上传。但是当我点击它查看它时,它显示在图像编号 2 中。
根据文档,请求的主体(又名 your CURLOPT_POSTFIELDS
)应该只包含[JPEG_DATA]
(也就是你的file_get_contents($uploadFile)
)。
但是你的样品中,你是给身体的数组'fileName'
和'fileData'
它甚至没有有效的身体属性的元数据的名称为谷歌云存储API JSON。
然后,Google Cloud Storage 将这个'fileName'
和数组解释'fileData'
为实际的[JPEG_DATA]
,并将其呈现为一个 JPEG 文件,该文件以您总是看到的小方形图像的形式返回。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句