I am very new to the google cloud storage.
I want to create folder in bucket using php coding. I have searched a quite few sites and on 1 i saw it was written:
Sometimes, in a CMS, you need to create a directory first before able to upload file into it, so the mouse click can trigger an event to take the path as the base folder, then do a batch upload.
It's a file browser, they say.
This code below might help.
setApplicationName('Create a new folder');
$client->setClientId($clientId);
$scopes = array('https://www.googleapis.com/auth/devstorage.full_control');
$client->setScopes($scopes);
$service = new Google_Service_Storage($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
if (!file_exists($privateKeyFile)) {
die('missing the location of primary key file, given: ' . $privateKeyFile);
}
$key = file_get_contents($privateKeyFile);
$cred = new Google_Auth_AssertionCredentials(
$clientEmailAddress
, $scopes
, $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
/**
* Creating Folder
*/
try {
/* create empty file that acts as folder */
$postBody = new Google_Service_Storage_StorageObject();
$postBody->setName($newDirectory);
$postBody->setSize(0);
$created = $service->objects->insert($bucketName, $postBody, array(
'name' => $newDirectory,
'uploadType' => 'media',
'projection' => 'full',
'data' => '',
));
} catch (Exception $ex) {
echo $ex->getMessage() . "\n";
print_r($ex->getTraceAsString());
echo '
';
die();
}
echo 'EOF';