title: Insert object to Google Cloud Storage at specific route using PHP
body: <p>I am amazed by the lack of information at the official documentation of Google Cloud Storage with PHP.</p>
  
  <p>Here we go... I'm trying to manage bucket's uploads and downloads with Google APIs Client Library for PHP, but I can't find anywhere the way to specify to the API where I want to upload my files at google's bucket (I can only upload them at bucket's root).</p>
  
  <p>Here's the code that I'm using for the upload (extracted from <a href="https://github.com/guillefd/Backup-manager-gcs/blob/master/application/libraries/Googlecloudstorage.php" rel="nofollow">https://github.com/guillefd/Backup-manager-gcs/blob/master/application/libraries/Googlecloudstorage.php</a>):</p>
  
  <pre><code>public function media_file_upload($file_path = null, $file_name = null) { 
  
  
      # init
      $result = new stdClass();
      $result-&gt;error = null;
      $result-&gt;status = null;
      $result-&gt;exception = null;
      # timer
      $result-&gt;starttime = microtime();
      # init gcs api
      $gso = new Google_Service_Storage_StorageObject();
      $gso-&gt;setName($file_name);
      $gso-&gt;setBucket($this-&gt;bucket);      
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
      $mimetype = finfo_file($finfo,$file_path);
      $chunkSizeBytes = 1 * 1024 * 1024;
      $this-&gt;client-&gt;setDefer(true);
      $filetoupload = array(
                              'name'=&gt;$file_name,
                              'uploadType'=&gt;'resumable'    
                          );       
      # service
      $this-&gt;newStorageService();
      $status = false;
      # try
      try {
          $request = $this-&gt;storageService-&gt;objects-&gt;insert($this-&gt;bucket, $gso, $filetoupload);
          $media = new Google_Http_MediaFileUpload($this-&gt;client, $request, $mimetype, null, true, $chunkSizeBytes);
          $media-&gt;setFileSize(filesize($file_path));
          $handle = fopen($file_path, "rb");
          # loop chunks
          while(!$status &amp;&amp; !feof($handle)) {
              $chunk = fread($handle, $chunkSizeBytes);
              $status = $media-&gt;nextChunk($chunk);
          }
          fclose($handle);
          $this-&gt;client-&gt;setDefer(false);
          // $result-&gt;status = $status;
      } catch(Exception $e) {
              $result-&gt;error = true;
              $result-&gt;status = 'Google Cloud Service upload failed';
              $result-&gt;exception = $e;
          }
      # timer
      $result-&gt;endtime = microtime();   
      $result-&gt;totaltime = $this-&gt;get_totaltime($result);
      # verify response
      $result-&gt;httpcode = http_response_code();
      $result-&gt;error = isset($status-&gt;kind) &amp;&amp; $status-&gt;kind==self::STORAGE_OBJECT ? false : true;        
      return $result;
  }
  </code></pre>
  
  <p>The main question that I'm actually asking is: Is there a way to upload files to google's bucket at a specific route, using Google Cloud's PHP API?</p>
  
  <p>Thanks!</p>