Search code examples
google-drive-apigoogle-api-php-client

Google Drive API can't set permissions with reusable upload


When I did a basic file upload & set the permission like so, it worked great:

$file = $service->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => $mimeType,
    'uploadType' => 'multipart',
    'fields' => 'id'));

$permissions = new Google_Service_Drive_Permission(array(
    "role" => "reader",
    "type" => "anyone",
));

$setPermission = $service->permissions->create($file->id, $permissions);

But when I upload a large file that's split into chunks & use the resumable uploadType, the permissions aren't set on the file & I don't get any errors:

$service = new Google_Service_Drive($client);

$file = new Google_Service_Drive_DriveFile();
$file->title = $fileName;
$file->name = $fileName;

$chunkSizeBytes = 1 * 1024 * 1024;
$mimeType = mime_content_type($fullpath);

// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);

// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
    $client,
    $request,
    $mimeType,
    null,
    true,
    $chunkSizeBytes
);
$media->setFileSize(filesize($fullpath));

// Upload the various chunks. $status will be false until the process is complete.
$status = false;
$handle = fopen($fullpath, "rb");
while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

// The final value of $status will be the data from the API for the object that has been uploaded.
$result = false;
if($status != false) {
    $result = $status;
    error_log($result->id);
}

fclose($handle);

$permissions = new Google_Service_Drive_Permission(array(
    "role" => "reader",
    "type" => "anyone"
));

$setPermission = $service->permissions->create($result->id, $permissions);

Any suggestions on what I'm doing wrong?


Solution

  • I have had the same situation with you. In that case, when the file information is retrieved with $res = $service->files->get($result->id); after the file was uploaded with the resumable upload, the empty object of {} is returned without error. I thought that this is the reason of the issue.

    So, I used the following workaround. In this workaround, after the file was uploaded with the resumable upload, I retrieved $client again. By this, $res = $service->files->get($result->id); worked after the file was uploaded. When this is used for your script, please modify as follows.

    From:

    fclose($handle);
    
    $permissions = new Google_Service_Drive_Permission(array(
        "role" => "reader",
        "type" => "anyone"
    ));
    
    $setPermission = $service->permissions->create($result->id, $permissions);
    

    To:

    fclose($handle);
    
    $client = getClient();  // <--- Added
    $service = new Google_Service_Drive($client);  // <--- Added
    
    $permissions = new Google_Service_Drive_Permission(array(
        "role" => "reader",
        "type" => "anyone"
    ));
    
    $setPermission = $service->permissions->create($result->id, $permissions);
    
    • getClient() is for retrieving $client. So please modify this part for your actual situation.

    Note:

    • This answer supposes that your $client can be created the permissions to the uploaded file. Please be careful this.