2016-02-21 63 views
2

批量上傳失敗使用谷歌驅動器V3與API的主分支(v2.0)。谷歌驅動V3,谷歌API客戶端2.0 - 批量上傳失敗

我修改了https://github.com/google/google-api-php-client/blob/master/examples/batch.php服務帳戶憑證。

的代碼:

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('[email protected]'); 
$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); 
} 

上面的代碼將在運行最後$結果= $分批>執行後失敗,出現 「未找到」(); (文件夾發票將成功創建)。

隨着$useBatch = false一切都按預期工作 - 一個文件夾中創建三個文件。

爲什麼在批量上傳時崩潰了?

謝謝!

+0

提起與谷歌的錯誤報告:https://github.com/google/google-api-php-client/issues/860 – temuri

回答

-1

基於Official Google Documentation,您收到'404文件未找到',原因是用戶沒有讀取權限或文件不存在。建議的操作:向用戶報告他們沒有對該文件的讀取權限或該文件不存在。告訴他們他們應該請求所有者許可文件。

您必須在您的request中包含'$ fileId'。另外,如果'$ useBatch = true',您應該設置'$ userPermission'。

注意:您應該使用v1-branch,因爲它是在這裏指出:https://github.com/google/google-api-php-client

+0

我沒有任何'$ fileId'。我正在上傳文件,而不是修改它們。所以'404文件找不到'似乎完全是隨機的,不合適的。 – temuri