【问题标题】:Google Cloud Platform - How to upload image file into google cloud storage bucket?谷歌云平台 - 如何将图像文件上传到谷歌云存储桶?
【发布时间】:2017-08-17 03:20:56
【问题描述】:

我正在尝试使用谷歌云存储 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>

Image Number 1

看图1,文件上传成功。但是当我点击它来查看它时,它会像第 2 幅图一样显示。

Image Number 2

【问题讨论】:

    标签: php google-cloud-platform google-cloud-storage json-api


    【解决方案1】:

    根据documentation 请求正文(也就是您的CURLOPT_POSTFIELDS)应该只包含[JPEG_DATA](也就是您的file_get_contents($uploadFile))。

    但在您的示例中,您为主体提供了 'fileName''fileData' 的数组,这对于 Google Cloud Storage JSON API 甚至是无效的 body property metadata names

    然后,Google Cloud Storage 会将这个 'fileName''fileData' 数组解释为实际的 [JPEG_DATA],并将其呈现为 JPEG 文件,该文件以您经常看到的小方形图像的形式返回。

    【讨论】:

      【解决方案2】:

      您的文件确实在浏览器的Image Number 2 中显示。

      由于您的图片尺寸很小,因此在您的浏览器中不容易看到。在your uploaded image 中,它显示了一个白色的小盒子,里面有四个盒子(2 个白色,2 个灰色)。您可以放大以确认。我相信那是你上传的图片。

      您可以尝试上传尺寸稍大的不同图片并确认它是否有效。

      【讨论】:

      • 我尝试上传更大尺寸的图像,但仍然显示相同
      猜你喜欢
      • 2018-07-11
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2016-04-26
      • 2015-03-01
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多