Trying to encyption file upload cloud storage through the curl in php

Viewed 32

Please help me how to encrypted file upload cloud storage in curl php i am trying this code but error show can help me how to solve this problem

<?php
    $ch = curl_init();
    $image = file_get_contents('2.jpg');
    $key = random_bytes(32);
    $encryptioinKey= base64_encode($key);
    $hash = hash('SHA256', $encryptioinKey);
    $BUCKET_NAME= "XXXXX";
    $oauth_token= "XXXX";
    curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/".$BUCKET_NAME."/o?uploadType=media&name=2.jpg");
    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/jpg', 'Authorization: Bearer ' . $oauth_token,'x-goog-encryption-algorithm:AES256','x-goog-encryption-key:'. $encryptioinKey,'x-goog-encryption-key-sha256:'.$hash));
    $result = curl_exec($ch);
    curl_close($ch);
    echo"<pre>";print_r($result);die();
     ?>


   when i  run this code then show this error solve this error 

      {
       "error": {
        "code": 400,
        "message": "Missing a SHA256 hash of the encryption key, or it is not base64 encoded, or it does not match the encryption key.",
     "errors": [
           {
              "message": "Missing a SHA256 hash of the encryption key, or it is not base64 encoded, or it does not match the encryption key.",
             "domain": "global",
              "reason": "customerEncryptionKeySha256IsInvalid",
              "extendedHelp": "https://cloud.google.com/storage/docs/encryption#customer-supplied_encryption_keys"
              }
          ]
      }
  }
0 Answers
Related