Search code examples
phpgoogle-cloud-platform

Creating folders using GCP PHP Client Libraries


I started learning Google Cloud Storage PHP Client Library through documentation given by Google. But following questions are uncleared.

  1. How to create a folder in a bucket?
  2. How to create a folder in a folder?
  3. How to list all the files in a folder?

I have learnt that folders are nothing but objects with "/" at the end of their name. Also we can upload an object using $bucket->upload() method to upload an object. This method needs a path of file so it is creating a trouble to create a folder


Solution

  • Answer to question1: Creating empty folders is possible only via cloud console. You cannot create empty folder in a GCP bucket via SDK functions. Suppose if you wanted to create, /test folder in a bucket, upload a file named /test/file.txt. Make sure that the files you upload later in that /test folder to be other than file.txt

    Answer to question2: Almost identical to first question except paths

    Answer to question3: Listing items in a particular directory can be done using extra parameters like delimiters, prefixes provided by GCP SDK.