1

Can I ask if how will I display the current storage used in my service account of Google Drive API?

I tried to call it in fields but its not working.

$parameters['fields'] = 'files(storage), nextPageToken';

I can't seem to find a post here by typing the title but I can't find any.

Mon Padi
  • 53
  • 1
  • 9

1 Answers1

3

How about using "About: get" of Drive API? When this is reflected to the PHP script using googleapis for PHP, it becomes as follows.

Sample script:

$service = new Google_Service_Drive($client); // Please use your "$client".
$res = $service->about->get(['fields' => 'storageQuota']);
$storageQuota = $res -> getStorageQuota();
print_r($storageQuota);

// When you want to retrieve the value of "usage` of "storageQuota", you can do it as follows.
$usage = $storageQuota -> getUsage();
echo $usage;

When above script is run, the following value is obtained.

{
  "limit": "###",
  "usage": "###",
  "usageInDrive": "###",
  "usageInDriveTrash": "###"
}

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • have you ever run this on a service account, whos limit is it going to show? does the service account have its own storage quota or does it go from the owner who create it? – Linda Lawton - DaImTo Jun 04 '21 at 07:40
  • 2
    @DaImTo Yes. I have already tested this for both OAut2 and the service account. In this case, `storageQuota` of each service account is returned. That is different from that of user's Google Drive. – Tanaike Jun 04 '21 at 07:46
  • 1
    Thanks i was just curious :) – Linda Lawton - DaImTo Jun 04 '21 at 08:01
  • hello again sir! I'm having an error https://imgur.com/OTZFSoq I used `foreach(` `$storageQuota as $k => $q ) {` `echo $q['usage'];` `}` Shoud I use foreach? Please do recommend – Mon Padi Jun 04 '21 at 08:01
  • @Mon Padi Thank you for replying. About `I'm having an error imgur.com/OTZFSoq I used foreach( $storageQuota as $k => $q ) { echo $q['usage']; } Shoud I use foreach?`, `storageQuota` is not an array. So when you want to retrieve `usage`, I added the script in my answer. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike Jun 04 '21 at 08:09
  • Thank you for replying too sir! But I'm still having an error https://imgur.com/ggeTf5B and I only copy & paste your code but it is still giving me an error. This is the code: https://imgur.com/QvC7pRV – Mon Padi Jun 04 '21 at 08:25
  • @Mon Padi Thank you for replying. I apologize for the inconvenience. I think that your script has already worked. In your situation, `0` of `usage` is returned as shown in [your image](https://imgur.com/ggeTf5B). And, I think that your error message is not related to my proposed script. When you want to check your warning, how about checking this thread? https://stackoverflow.com/q/48001569 – Tanaike Jun 04 '21 at 08:51
  • Hello sir @Tanaike It works! Thank you again for your help :) May I know what is this number 16106127360, Is this megabyte(mb) or bytes? – Mon Padi Jun 07 '21 at 01:27
  • 1
    @Mon Padi Thank you for replying. I'm glad your issue was resolved. And, in that case, the unit is "bytes". – Tanaike Jun 07 '21 at 01:28