dongmeng2509 2015-03-23 14:43
浏览 68
已采纳

上传的文件内容缺少Google云端硬盘

Info:

I've made a short sample in which I upload a file to Google Drive using the credentials of the service account. Since the service account doesn't have a Drive UI (Google Drive API Upload returns File ID/Title but document does not exist in Drive), I've added permissions for my own account. This works fine. But when I open the file in Google Docs I don't see the content of the file I uploaded. Below is my code. In the file I want to upload the content is "Hello World!" but this is not present in the document on Google Drive. Any ideas on how to fix this?

        function insertFile($service, $title, $description, $parentId, $mimeType, $fileName) {
            print "<br/>Uploading file...<br/>";
            $file = new Google_Service_Drive_DriveFile();
            $file->setTitle($title);
            $file->setDescription($description);
            $file->setMimeType($mimeType);

            // Set the parent folder
            if ($parentId != null) {
                $parent = new Google_Service_Drive_ParentReference();
                $parent->setId($parentId);
                $file->setParents(array($parent));
            }

            try {
                $data = file_get_contents($fileName);

                $createdFile = $service->files->insert($file, array(
                    'data' => $data,
                    'mimeType' => $mimeType
                ));

                $newFileId = $createdFile->getId();

                // Give permission to my email so I can see the file in my Drive UI
                $myEmail = "<mygmail>";
                $userType = "user";
                $userRole = "writer";
                insertPermission($service, $newFileId, $myEmail, $userType, $userRole);

                print "<br/>File uploaded successfully!";
                downloadFile($service, $newFileId);
            } catch (Exception $e) {
                print "<br/>An error occurred: " . $e->getMessage();
            }
        }

        function insertPermission($service, $fileId, $userToPermit, $typeOfUser, $role) {
            $newPermission = new Google_Service_Drive_Permission();
            $newPermission->setValue($userToPermit);
            $newPermission->setType($typeOfUser);
            $newPermission->setRole($role);
            try {
                return $service->permissions->insert($fileId, $newPermission);
            } catch (Exception $e) {
                print "<br/>Error occurred while attempting to permit " . $userToPermit . " to file.<br/>" . $e->getMessage();
            }
            return null;
        }

        $docTitle = "UploadTest4.txt";
        $docMimeType = "text/plain";
        $docToUpload = "document.txt";
        insertFile($service, $docTitle, null, null, $docMimeType, $docToUpload);
  • 写回答

1条回答 默认 最新

  • dq62957 2015-03-23 14:52
    关注

    Didn't find this post before, but I tried the solution provided and it works for me (not for the original poster strangely enough). I had to add an uploadType parameter in the insert request.

    uploadType => 'media'
    

    file uploaded with empty content using google drive php api

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条