Google Drive V3,Google API客户端2.0 - 批量上传失败 [英] Google Drive V3, Google API client 2.0 - batch upload is failing

查看:292
本文介绍了Google Drive V3,Google API客户端2.0 - 批量上传失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Drive V3和API主分支(v2.0)批量上传失败。



我修改了 https://github.com/google/google-api-php-client/blob/master /examples/batch.php 与服务帐户凭证。



代码:

  include_once __DIR__。 /../vendor/autoload.php; 
include_oncetemplates / base.php;

echo pageHeader(Batching Queries);

//在批次处理和连续上传之间使用TRUE或FALSE切换。
$ useBatch = true;

$ client = new Google_Client();
$ client-> setScopes([
'https://www.googleapis.com/auth/drive',
]);
if($ credentials_file = checkServiceAccountCredentialsFile()){
//手动设置位置
$ client-> setAuthConfig($ credentials_file);
} elseif(getenv('GOOGLE_APPLICATION_CREDENTIALS')){
//使用应用程序默认凭证
$ client-> useApplicationDefaultCredentials();
} else {
exit;
}
$ client-> setSubject('some@email.com');
$ service = new Google_Service_Drive($ client);
$ client-> setUseBatch($ useBatch);

if($ useBatch){
$ batch = $ service-> createBatch();


$ folder = new Google_Service_Drive_DriveFile([
'name'=>'Invoices',
'mimeType'=>'application / vnd.google -apps.folder'
]);

$ req = $ service-> files-> create($ folder,[
'fields'=>'id'
]);
$ b $ if($ useBatch){
$ result = $ batch-> add($ req,'newfolder');
$ folder = $ batch-> execute()['response-newfolder'];
$ newFolderId = $文件夹 - > id;
} else {
$ newFolderId = $ req-> id;
}

$ uploadIDs = null;

if($ useBatch){
$ batch = $ service-> createBatch();


$ b($ i = 1; $ i <= 3; $ i ++){
$ file = new Google_Service_Drive_DriveFile([
'name'=> ; $ i。'.jpg',
'mimeType'=>'image / jpeg',
'parents'=> [$ newFolderId],
]);

$ req = $ service-> files-> create($ file,[$ b $'data'=> file_get_contents('img /'.$ i。'。jpg' ),
'mimeType'=>'image / jpeg',
'uploadType'=>'media',
'fields'=>'id',
]);

if($ useBatch){
$ batch-> add($ req,$ i);
} else {
$ uploadIDs [] = $ req-> id;


$ b $ if($ useBatch){
$ results = $ batch-> execute();
} else {
print_r($ uploadIDs);
}

上面的代码在运行最后一个$ results = $分批>执行(); (文件夹发票将成功创建)。



使用 $ useBatch = false 一切都按预期工作 - 创建了三个文件。



为什么在批量上传时崩溃?



谢谢!

解决方案

根据 Google官方文档,您收到' 404文件未找到',原因是用户没有对文件的读取权限或文件不存在。建议的操作:向用户报告他们没有对该文件的读取权限或该文件不存在。告诉他们,他们应该请求所有者许可文件。



您必须将 $ fileId 包含在请求。此外,如果' $ useBatch = true ',则应该设置' $ userPermission '。注意:您应该使用 v1分支,因为它是在这里陈述的: https://github.com/google/google-api-php-client

Batch upload fails using Google Drive V3 with master branch of API (v2.0).

I've modified https://github.com/google/google-api-php-client/blob/master/examples/batch.php with service account credentials.

The code:

include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";

echo pageHeader("Batching Queries");

// USE TRUE OR FALSE TO TOGGLE BETWEEN BATCHED AND SEQUENTIAL UPLOADS.
$useBatch = true;

$client = new Google_Client();
$client->setScopes([
    'https://www.googleapis.com/auth/drive',
]);
if ($credentials_file = checkServiceAccountCredentialsFile()) {
  // set the location manually
  $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
  // use the application default credentials
  $client->useApplicationDefaultCredentials();
} else {
  exit;
}
$client->setSubject('some@email.com');
$service = new Google_Service_Drive($client);
$client->setUseBatch($useBatch);

if ($useBatch) {
    $batch = $service->createBatch();
}

$folder = new Google_Service_Drive_DriveFile([
  'name' => 'Invoices',
  'mimeType' => 'application/vnd.google-apps.folder'
]);

$req = $service->files->create($folder, [
  'fields' => 'id'
]);

if ($useBatch) {
    $result = $batch->add($req, 'newfolder');
    $folder = $batch->execute()['response-newfolder'];
    $newFolderId = $folder->id;
} else {
    $newFolderId = $req->id;
}

$uploadIDs = null;

if ($useBatch) {
    $batch = $service->createBatch();
}

for ($i=1;$i<=3;$i++) {
    $file = new Google_Service_Drive_DriveFile([
        'name' => $i . '.jpg',
        'mimeType' => 'image/jpeg',
        'parents' => [$newFolderId],
    ]);

    $req = $service->files->create($file, [
        'data' => file_get_contents('img/'.$i.'.jpg'),
        'mimeType' => 'image/jpeg',
        'uploadType' => 'media',
        'fields' => 'id',
    ]);

    if ($useBatch) {
        $batch->add($req, $i);
    } else {
        $uploadIDs[] = $req->id;
    }
}

if ($useBatch) {
    $results = $batch->execute();
} else {
    print_r($uploadIDs);
}

The code above will fail with "Not Found" after running last $results = $batch->execute(); (folder Invoices will be created successfully).

With $useBatch = false everything works as expected - a folder is created with three files in it.

Why is it crashing on batch upload?

Thanks!

解决方案

Based on the Official Google Documentation, you received '404 File not found' due to user does not have read access to a file or the file does not exist. Suggested action: Report to users that they do not have read access to the file or that the file does not exist. Tell them that they should ask the owner for permission to the file.

You have to include your '$fileId' in your request. Also, you should set '$userPermission' if '$useBatch=true'.

Note: You should use the v1-branch as it is stated here: https://github.com/google/google-api-php-client

这篇关于Google Drive V3,Google API客户端2.0 - 批量上传失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆