-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.php
67 lines (60 loc) · 2.32 KB
/
storage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class storage {
private $projectId;
private $storage;
public function __construct() {
putenv("GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/credentials/learnWebCoding-636c1b188316.json");
$this->projectId = 'learnwebcoding';
$this->storage = new StorageClient([
'projectId' => $this->projectId
]);
$this->storage->registerStreamWrapper();
}
public function createBucket($bucketName) {
$bucket = $this->storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
}
public function listBuckets() {
$buckets = $this->storage->buckets();
foreach ($buckets as $bucket) {
echo $bucket->name() . PHP_EOL;
}
}
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);
}
function listObjects($bucketName) {
$bucket = $this->storage->bucket($bucketName);
foreach ($bucket->objects() as $object) {
printf('Object: %s' . '<br>', $object->name());
}
}
function deleteObject($bucketName, $objectName, $options = []) {
$bucket = $this->storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->delete();
printf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
}
function deleteBucket($bucketName) {
$bucket = $this->storage->bucket($bucketName);
$bucket->delete();
printf('Deleted gs://%s' . PHP_EOL, $bucketName);
}
function downloadObject($bucketName, $objectName, $destination) {
$bucket = $this->storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->downloadToFile($destination);
printf('Downloaded gs://%s/%s to %s' . PHP_EOL,
$bucketName, $objectName, basename($destination));
}
function getImageUrl($bucketName, $objectName) {
return 'https://storage.cloud.google.com/'.$bucketName.'/'.$objectName;
}
}