4
votes

I am using the following link for using the Google Cloud Storage :

Google CLoud

I want to upload an object using insert function given in the above API.I am using PHP. The code i am using is as follows :

$StorageService = new Google_StorageService($client);
$objects = $StorageService->objects;
$gso = new Google_StorageObject();
$gso->setName('myobj');
$postbody = array(file_get_contents('buc.jpg'));
$resp = $objects->insert('mybucket', $gso, $postbody);

But i m getting Error as :

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/storage/v1beta1/b/mybucket/o: (400) Required' in /home/www/public_html/abc/google-api-php-client/src/io/Google_REST.php on line 66

What am i doing wrong please help me out..

There is some problem with the way i am sending the parameters. Please if anyone knows help me..

There is an example here :

Example

But that is in Java Please help me to do the same thing in PHP. Please help me out.

3

3 Answers

4
votes

I figured out the answer. The code to be used is as follows :

$objects = $StorageService->objects;
$postbody = array('data' => file_get_contents('buc.jpg'));
$gso = new Google_StorageObject();
$gso->setName('mybuc');
$resp = $objects->insert('mybucket', $gso ,$postbody);
print_r($resp);

and then it works .. I was not setting the parameter "data" in an array to be passed as a third parameter of the function. I Found out and it worked.

0
votes

Buckets share a global namespace. You have to create your bucket before storing objects in it, and "mybucket" is not an available name.

If you go to the GCS browser at https://storage.cloud.google.com/, you can create your bucket there and then reference the existing bucket in your PHP code.

0
votes

I had to add uploadType to get it to work as mentioned here: https://cloud.google.com/storage/docs/json_api/v1/objects/insert

$postbody = array('uploadType' => 'media', 'data' => file_get_contents('buc.jpg'));

Where is the data parameter documented?