Unable to upload to Google Cloud Storage using Google APIs

I use the following link to use Google Cloud Storage:

Google cloud

I want to load an object using the insert function specified in the above API. I am using PHP. The code I use 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 get an error like:

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

What am I doing wrong, help me ...

There is some problem with the way I submit the parameters. Please, if anyone knows, help me ..

Here is an example:

Example

But this is in Java. Please help me do the same in PHP. Please help me.

+4
source share
3 answers

I understood 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 did not set the "data" parameter in the array, which should be passed as the third parameter to the function. I found out and it worked.

+4
source

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

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

0
source

I had to add uploadType to make it work as indicated 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 registered?

0
source

Source: https://habr.com/ru/post/1435885/


All Articles