0
votes

Here's something fun. Our system includes a file manager for our users. The files are being stored in a bucket in Google Cloud Storage. What I need to do is create a "folder" in the bucket. Yes, I know that GCS is a flat space, and folders aren't really a thing... but I can go to the Google Cloud Platform browser and click on a button called "CREATE FOLDER", and it creates something that looks just like a folder. There shouldn't be any content to it. I am trying to use cURL to write something the storage object so that it is "saved":

$bucket = $google_client( $bucket_name );
$folder = $bucket->object( $folder_name );

$url = $folder->beginSignedUploadSession();

// now we have to make an HTTP "PUT"
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_PUT, 1);
curl_setopt($ch,CURLOPT_INFILE, "");   <--- this is wrong
curl_setopt($ch,CURLOPT_INFILESIZE, 0);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Or should I just create dummy "file" to append to the URL I'm creating with a dummy value in it?

I suppose I should add that I am working with an existing system that does this using an older version of the GCS API, using code like:

$postbody = [ 'data' => $data ,
              'uploadType' => "multipart", 
              'mimeType' => 'text/plain'];
$gso = new Google_Service_Storage_StorageObject();
$gso->setName( $foldername );
$resp = $objects->insert( $bucket, $gso, $postbody );
1
Empty folders are not really a thing in Cloud Storage. The concept of a folder exists only to help you organize your actual objects. What is your goal in having an empty folder appear?Doug Stevenson
As I stated, we have created a "File Manager" in our system. Our users, who pay our bills, expect to be able to create "folders" and fill them with "files". I can do exactly this using the Google Cloud Platform console. I just want to be able to replicate this facility for display.Andy Wallace
You should probably use a database to store the folder structure that you want your users to see. Use Cloud Storage to store the actual object content. As you said in the question, "Yes, I know that GCS is a flat space", so I would advise trying use it for another other than that.Doug Stevenson

1 Answers

0
votes

Ok, I have an answer now. You can use the bucket->upload function. You need to specify NULL as the data value, and use options to specify the full path. Make sure that the folder name ends with a slash ("/"), or you'll get an empty file object.

$folder_name = "level1/level2/folder/";
$options = [ 'name' => $folder_name ];
$upload_result = $bucket->upload( NULL, $options );

Note: if level1 and level2 don't exist, they will also be created as folders, giving you that implicit hierarchy.