Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating folder in bucket google cloud storage using php

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:

"Creating a folder inside a bucket will create a placeholder object named after the directory, has no data content and the mimetype application/x-directory. Directory placeholder objects created in Google Storage Manager are ​not supported."

I could not understand what it is trying to say. How can i create folder please help me out. I tried using the following code:

$req = new Google_HttpRequest("http://commondatastorage.googleapis.com/bucket/myfoldertrial");
$req->setRequestHeaders(array(
'x-goog-project-id' => 21212,
'x-goog-acl' => 'public-read',
'Content-Type' => 'application/x-directory'
));
$req->setRequestMethod('PUT');
$req->setPostBody('myfoldertrial');

I am using the API from following link:

Google API for PHP

Please help me out creating folder using PHP.

like image 411
Sanober Malik Avatar asked Sep 21 '12 09:09

Sanober Malik


2 Answers

You probably don't actually need to create a folder.

Google Storage isn't a tree structure like your operating system's filesystem uses, all Objects are stored in buckets at the top level. However you can give an Object a name with slashes in it, so it will kind of look like it is in a folder - Users/username/docs/2012/09/21/activity.csv is a perfectly good name for an Object and doesn't need any supporting folders.

Once you've got Objects with this sort of scheme in place, you can list them as if you were viewing the contents of a folder with the delimiter and prefix parameters as per these docs.

So if you only wanted to create myfoldertrial so you could upload example.png into it, there's no need to create the folder, you can just upload straight to myfoldertrial/example.png.

like image 134
Cebjyre Avatar answered Sep 21 '22 02:09

Cebjyre


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.

<?php

$privateKeyFile = '{{full/path/to/*.p12}}';
$newDirectory = '{{path/of/new/directory/}}'; // remember to end it with a slash.

/**
 * Authentication
 */
$client = new Google_Client();
$client->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<pre>";
    print_r($ex->getTraceAsString());
    echo '</pre>';
    die();
}

echo 'EOF';
like image 45
goldsky Avatar answered Sep 20 '22 02:09

goldsky