PHP code example of simplon / gstorage
1. Go to this page and download the library: Download simplon/gstorage library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
simplon / gstorage example snippets
$credentials = (new ServerAccountCredentials())->loadFromParams(
"cloud-storage-web-server@api-project-XXXXXXXXXXXXXX.foo.bar.iam.gserviceaccount.com",
"-----BEGIN PRIVATE KEY-----\nXXXXXXXXXXXXXX\n-----END PRIVATE KEY-----\n"
);
$credentials = (new ServerAccountCredentials())->loadFromJsonFile('credentials.json');
$gstorage = new Gstorage($credentials);
// upload object
$data = new UploadData('YOUR-BUCKET-NAME');
// load file via URL
$data->loadWithFile('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
// upload file
$objectData = $gstorage->upload($data); // ObjectData|null
if ($objectData)
{
var_dump([
'bucket' => $objectData->getBucket(),
'id' => $objectData->getFileName(),
'url_public' => $objectData->getUrlPublic(),
]);
}
// our blob
$blob = file_get_contents('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
// upload object
$data = new UploadData('YOUR-BUCKET-NAME');
// load file via BLOB
$data->loadWithBlob('google-logo.png', $blob);
// upload file
$objectData = $gstorage->upload($data); // ObjectData|null
if ($objectData)
{
var_dump([
'bucket' => $objectData->getBucket(),
'id' => $objectData->getFileName(),
'url_public' => $objectData->getUrlPublic(),
]);
}
$response = $gstorage->delete(
new ObjectData('YOUR-BUCKET-NAME', 'google-logo.png')
);
var_dump($response); // true|false
$gstorage = new Gstorage(
(new ServerAccountCredentials())->loadFromJsonFile('credentials.json')
);
// upload object
$data = new UploadData('YOUR-BUCKET-NAME');
// load file via URL
$data->loadWithFile('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
// upload file
$objectData = $gstorage->upload($data); // ObjectData|null
if ($objectData)
{
var_dump([
'bucket' => $objectData->getBucket(),
'id' => $objectData->getFileName(),
'url_public' => $objectData->getUrlPublic(),
]);
}