首页 > 解决方案 > 无法通过 Google Compute Engine 使用 PHP 在 Google Cloud Storage 上上传文件

问题描述

我正在尝试将图像文件上传到 GCS 中的存储桶,只在我的页面上插入了一个文件输入和一个按钮进行一些测试,当我单击该按钮时,它什么也没做,没有错误,什么也没有。我在 Google Cloud Platform Compute Engine 中使用 Ubuntu VM。实际上我不能用谷歌云存储客户端做任何事情,不能上传对象,列出对象,列出存储桶,我已经把它上传到我的网站而且它不起作用,首先在 localhost 上尝试但它给了我这个错误:cURL 错误 60:SSL 证书问题:无法获取本地颁发者证书,这就是我将其上传到 GCP 上的 VM 的原因。

<?php
require_once __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class googleStorage{
    private $projectId;
    private $storage;
    public function __construct() {
        //putenv("GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/vendor/google/Cloud/credentials.json");        
        $this->projectId = 'my_project_id';
        $this->storage = new StorageClient([
            'projectId' => $this->projectId
        ]);
        $this->storage->registerStreamWrapper();
    }

    function uploadObject($bucketName, $objectName, $source) {
        $file = fopen($source, 'r');
        $bucket = $this->storage->bucket($bucketName);
        $object = $bucket->upload($file, [
            'name' => $objectName
        ]);
        printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
    }

    public function listBuckets() {
        $buckets = $this->storage->buckets();
        foreach ($buckets as $bucket) {
            echo $bucket->name() . PHP_EOL;
        }
    }

    function listObjects($bucketName) {
        $bucket = $this->storage->bucket($bucketName);
        foreach ($bucket->objects() as $object) {
            printf('Object: %s' . '<br>', $object->name());
        }
    }

    function getImageUrl($bucketName, $objectName) {
        return 'https://storage.cloud.google.com/'.$bucketName.'/'.$objectName;
    }

}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap Select-->
    <link rel="stylesheet" href="vendor/bootstrap-select/css/bootstrap-select.min.css">
    <title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="foto" class="form-control">
    <button type="submit" name="upload" class="form-control">Upload</button>
</form>
<?php
    include "googleStorage.php";
    $bucket = "my_bucket";
    $storage = new googleStorage();
    $storage->listBuckets();
    $storage->listObjects($bucket);
    $imageUrl = $storage->getImageUrl($bucket, 'stitch.jpg');


    if(isset($_POST['upload'])){
        $storage->uploadObject($bucket, $_FILES['foto']['name'], $_FILES['foto']['tmp_name']);
    }    
?>   
</body>
</html>

标签: phpgoogle-cloud-storagegoogle-compute-engine

解决方案


要使 Compute Engine vm 实例能够访问 Cloud Storage,请执行以下步骤:

  1. 转到计算引擎
  2. 单击您的虚拟机实例
  3. 检查最底部的Cloud API 访问范围。

注意:如果设置为Allow default access,则必须通过以下步骤进行更改:

  1. 停止您的虚拟机并单击编辑

  2. 转到Cloud API 访问范围

  3. 选择以下选项:

    A.允许完全访问所有 Cloud API,以访问所有 Cloud API,包括 Cloud Storage。

    B.为每个 API 设置访问权限,只有选定的 Cloud API 才有访问权限。

  4. 保存启动/恢复您的虚拟机。


推荐阅读