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>