J.Rei Marco
J.Rei Marco

Reputation: 50

Is it possible to upload files to google cloud storage with just Authorization Bearer Token and Bucket name? (php code)

Currently I could upload files in google cloud storage with my service account credentials.

But ideally, if it is possible, I would like to upload files in google cloud storage with just bearer token and bucket name.

Upvotes: 1

Views: 1082

Answers (1)

pessolato
pessolato

Reputation: 1562

I believe what you are looking for are the JSON and XML APIs. These allow upload to Google Cloud Storage with only the Authorization Bearer header. You can find more information about how to upload objects through these API in the Uploading objects documentation.

You will need to curl from PHP in order to upload the files, by doing something like the code snippet below:

$ch = curl_init();
$image = file_get_contents('my_image.jpeg');

curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=my_image.jpeg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $image); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: image/jpeg', 'Authorization: Bearer ' . $oauth_token)); 

$result = curl_exec($ch);
curl_close($ch);

Upvotes: 1

Related Questions