简体   繁体   English

使用服务帐户上传的Google云端硬盘始终返回403(存储空间配额)

[英]Google Drive upload with a service account always returns 403 (Storage Quota)

I wrote some logic to upload files into a Google Drive folder which is shared to a service account (uploader@uploadproject-204816.iam.gserviceaccount.com) 我写了一些逻辑将文件上传到共享给服务帐户(uploader@uploadproject-204816.iam.gserviceaccount.com)的Google云端硬盘文件夹中

The Google Drive itself is associated with my G Suite account. Google云端硬盘本身已与我的G Suite帐户关联。

This worked for a while, but eventually I started getting this error: 这工作了一段时间,但最终我开始出现此错误:

Google.Apis.Requests.RequestError The user's Drive storage quota has been exceeded. Google.Apis.Requests.RequestError已超出用户的云端硬盘存储配额。 [403] Errors [ Message[The user's Drive storage quota has been exceeded.] Location[ - ] Reason[storageQuotaExceeded] Domain[global] ] [403]错误[消息[已超出用户的云端硬盘存储配额。]位置[-]原因[storageQuotaExceeded]域[全局]]

Ever since, the upload is not working, getting this error even when uploading a puny file. 从那时起,上传无法正常工作,即使上传的文件很少,也会出现此错误。

Using my real account, I cleaned up all the files owned by the service account, even those that were "unorganized". 使用我的真实帐户,我清理了服务帐户拥有的所有文件,甚至是那些“无组织的”文件。

The quota to the left says "170MB of 30GB used" 左侧的配额为“已使用170MB 30GB”

I also waited more than 24hrs. 我还等了24小时以上。

I think this is about the service account itself, not the drive. 我认为这与服务帐户本身有关,而不与驱动器有关。 But I'm not sure what to check. 但是我不确定要检查什么。

I tried inspecting the results of the About.Get() method, didn't find anything useful. 我尝试检查About.Get()方法的结果,没有发现任何有用的信息。

var cred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(clientEmail)
{
            Scopes = (new[] { "https://www.googleapis.com/auth/drive" }).ToList()
}.FromPrivateKey(privateKey));

service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer { ApiKey = apiKey, HttpClientInitializer = cred });
var about = service.About.Get();

EDIT: 编辑:

Turns out I needed to do something else to get more information about the quota 原来我需要做些其他事情才能获得有关配额的更多信息

var get = service.About.Get();
get.Fields = "*";
var about = get.Execute();

Then I can see in about.StorageQuota 然后我可以在about.StorageQuota看到

Limit: 16106127360
Usage: 16106125128

So indeed, I understand the storage quota error now. 所以确实,我现在了解存储配额错误。 The question is, how do I reset these numbers ? 问题是,如何重置这些数字? All the files the service account uploaded were deleted by MY account in the same location. 服务帐户上载的所有文件均被我的帐户在同一位置删除。

Any ideas ? 有任何想法吗 ?

It turns out to lower the storage usage for the service account, files need to be deleted with the service account as well. 事实证明,这降低了服务帐户的存储使用量,文件也需要使用服务帐户删除。

When does the files get deleted from Google Drive Service account? 文件何时从Google云端硬盘服务帐户中删除?

you can't delete file created (so owned) by account service with your real account. 您无法删除使用真实帐户通过帐户服务创建(拥有)的文件。

even you did that, you'll not see the files on your real account,but they still on the service account. 即使您这样做了,您也不会在真实帐户中看到文件,但仍会在服务帐户中看到它们。

you have to delete them with the account service. 您必须使用帐户服务将其删除。

below a script explained to delete file with PHP. 下面的脚本说明了如何使用PHP删除文件。 for this you have to know the ID of files you want to delete. 为此,您必须知道要删除的文件的ID。 so we make request to get all files IDs contained in folder parent referenced by 0B1-GGKdq5A3qX1dzNkhOLW1VWVU (each file and foler is referenced by unique UID like this), and then make a loop todelete every file. 因此,我们要求获取由0B1-GGKdq5A3qX1dzNkhOLW1VWVU引用的父文件夹中包含的所有文件ID(每个文件和文件夹都由唯一的UID引用,例如这样),然后进行循环删除每个文件。

first create client to connect to service account 首先创建客户端以连接到服务帐户

$drive_service = new Google_Service_Drive($client);

second, get all files in folder referenced by 0B1-GGKdq5A3qX1dzNkhOLW1VWVU 第二,获取0B1-GGKdq5A3qX1dzNkhOLW1VWVU引用的文件夹中的所有文件

$files_list = $drive_service->files->listFiles(array(
                'fields' =>  'files(id, name)',
                'q' => "'0B1-GGKdq5A3qX1dzNkhOLW1VWVU' in parents" , 
);)  ;
$files_list = $files_list->getFiles() ;

then make loop to delete all files 然后进行循环以删除所有文件

foreach ($files_list as $file){
        $id = $file->id;
        $resp = $drive_service->files->delete($id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM