-5

I'm trying to upload image files to my bucket in GCS, only inserted a file input and a button on my page for some testing and when I click the button, it doesn't do anything, no errors, nothing. I'm using an Ubuntu VM in Google Cloud Platform Compute Engine. Actually I can't do anything with the Google Cloud Storage Client, can't upload objects, list objects, list buckets, I already uploaded it to my website and it doesn't work, first tried on localhost but it gave me this error: cURL error 60: SSL certificate problem: unable to get local issuer certificate, that's why I uploaded it to my VM on GCP.

<?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>
JM Gelilio
  • 3,482
  • 1
  • 11
  • 23
Erik
  • 3
  • 3
  • 1
    The error did not originate from Cloud Storage or Google Cloud. Check this [question](https://stackoverflow.com/q/42094842/8753991) there are 9 answer that you should try. – JM Gelilio Mar 23 '21 at 08:36
  • 4
    Does this answer your question? [cURL error 60: SSL certificate in Laravel 5.4](https://stackoverflow.com/questions/42094842/curl-error-60-ssl-certificate-in-laravel-5-4) – Soni Sol Mar 23 '21 at 23:21
  • I know the problem on localhost didn't originate form Google Cloud, however, I uploaded the file to my VM on Google Compute Engine and I can't upload a file, so my problem is that I can't upload a picture with PHP to Google Cloud Storage, the cURL error only happens on localhost but I want to upload files from my website, thanks for your answer @JohnMichaelGelilio but it doesn't resolve my problem – Erik Mar 24 '21 at 17:38
  • No @SoniSol it doesn't answer my question – Erik Mar 24 '21 at 17:40
  • @Erik Go to your [Compute Engine](https://console.cloud.google.com/compute/instances) click your vm instance then check your the Cloud API access scopes. If it is set to default try to set it to Allow full access to all Cloud APIs, your vm instance will have an access to all Cloud API including Cloud Storage. Note, you cannot edit the Cloud API access scopes, you must stop the vm first. – JM Gelilio Mar 25 '21 at 00:20
  • Thank you @JohnMichaelGelilio that was the problem, as soon as I changed the Cloud API Access Scopes the file was uploaded immediately. – Erik Mar 26 '21 at 04:53
  • Good to know, I will provide an answer to help others if they encounter the same issue. – JM Gelilio Mar 26 '21 at 05:03

1 Answers1

0

For Compute Engine vm instance to have access to Cloud Storage, follow the steps below:

  1. Go to Compute Engine
  2. Click your vm instance
  3. Check Cloud API access scopes at the very bottom.

Note: If it is set to Allow default access, you must change it by the following steps:

  1. Stop your VM and click Edit

  2. Go to Cloud API access scopes

  3. Select the option below:

    A. Allow full access to all Cloud APIs, to have an access to all Cloud API including Cloud Storage.

    B. Set access for each API, only selected Cloud API will have an access.

  4. Save and Start/Resume your vm.

JM Gelilio
  • 3,482
  • 1
  • 11
  • 23