首页 文章

如何仅将文件夹上传到Google Cloud 端硬盘

提问于
浏览
3

tl; dr:无法将空文件夹上传到Google Cloud 端硬盘 . 谷歌API不清楚,需要帮助 .

我正在尝试创建一个可以将空文件夹上传到Google Cloud 端硬盘的程序 . 我查看了Google Drive API处理文件夹,但目前尚不清楚,所以如果你们能给出更明确的答案,我们将不胜感激!

我尝试从Google转换源代码以上传文件,即body.mimeType =“application / vnd.google-apps.folder”;等等 . 我得到的错误是UnauthorizedAccessException,因为你不能只将文件夹转换为byteArray .

我对C#知之甚少,所以我不知道如何解决这个问题 . 如果你也可以包含代码片段,以便我可以想象放置代码的位置,我也会很感激!

码:

Google.Apis.Drive.v2.Data.File folder = new Google.Apis.Drive.v2.Data.File();
        folder.Title = My Folder;
        folder.Description = desc.;
        folder.MimeType = applicationvnd.google-apps.folder;
       //Tried ^ that, didn't work

        FilesResource.InsertMediaUpload request = service.Files.Insert(folder).Fetch();
        Google.Apis.Drive.v2.Data.File file = service.Files.Insert(folder).Fetch();
        File file = service.Files.Insert(folder).Fetch();
       //Again, tried those but that didn't work either

        File body = new File();
        body.Title = "Document";
        body.Description = "A test folder";
        body.MimeType = "application/vnd.google-apps.folder";

        byte[] byteArray = System.IO.File.ReadAllBytes("Folder");
        //I know this doesn't work but had to try anyways
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.folder");
        request.Upload();

        File file = request.ResponseBody;
        Console.WriteLine(File id  + file.Id);

我认为这是我尝试过的大部分内容,但每个都没有用 . 还有其他我尝试但我已经删除了代码,因为它不起作用,重新找到它是没有意义的 .

谢谢!

1 回答

  • 2

    我解决了它(通过在另一个问题上找到答案):

    而不是File file = service.Files.Insert(body).Fetch(); (很多人说这不起作用,),使用File file = service.Files.Insert(body) . Execute (); Upvote(或论此论坛有效)答案:Create folder on Google Drive .NET

    码:

    File body = new File();
            body.Title = "document title";
            body.Description = "document description";
            body.MimeType = "application/vnd.google-apps.folder";
    
            // service is an authorized Drive API service instance
            File file = service.Files.Insert(body).Execute();
    
            //File body = new File();
            //body.Title = "My document";
            //body.Description = "A test document";
            //body.MimeType = "text/plain";
    
            //byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
            //System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
    
            //FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
            //request.Upload();
    
            //File file = request.ResponseBody;
    

    我把我注释掉的所有东西都放进去了,因为我发现它有助于查看代码是什么,并且不包括在内 . 此前源代码中的所有内容(请参阅Google上的快速入门C#以供参考)完全相同 .

    希望这可以帮助其他有同样问题的人!

相关问题