替换Google Drive UpdateMediaUpload中的文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换Google Drive UpdateMediaUpload中的文件相关的知识,希望对你有一定的参考价值。
我不知道,为什么文件的内容不会被更新。有人提示吗?
DriveFile.Id有效。源文件是现有可读文件。创建和删除文件可以正常工作。但不是更新。
我已经阅读了从v2到v3的迁移,使用了Generell的Http-PATCH方法进行更新。这是答案吗?
我不想删除该文件并创建一个新文件。
public long DriveUpdateFile(string fileID, string filename, string description, string parent, string mimeType)
{
int lError = (int)Win32ErrorCode.ERROR_SUCCESS;
if (System.IO.File.Exists(filename))
{
FilesResource.GetRequest get = m_Drive.Files.Get(fileID);
File body = get.Execute();
if(!string.IsNullOrEmpty(description))
body.Description = description;
if (!string.IsNullOrEmpty(mimeType))
body.MimeType = mimeType;
// v3 Sematics
if (!string.IsNullOrEmpty(parent))
{
body.Parents = new List<string>();
body.Parents.Add(parent);
}
try
{
// File's content.
using (System.IO.FileStream sr = System.IO.File.OpenRead(filename))
{
FilesResource.UpdateMediaUpload request = m_Drive.Files.Update(body, body.Id, sr, body.MimeType);
request.Upload();
}
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
lError = e.HResult;
}
}
else
{
Console.WriteLine("File does not exist: " + filename);
lError = (int)Win32ErrorCode.ERROR_FILE_NOT_FOUND;
}
return (lError);
}
答案
使用如下范围,
string[] scopes = new string[] {
DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadata,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveScripts
};//DriveService.Scope.DriveReadonly
并且我检查上面的代码。问题是ParentFolderId。请删除此行
body.Parents = new List<string>();
body.Parents.Add(parent);
在此范围内不允许直接分配。它应该与请求对象一起提及:[request.Parent ... bla bla
并尝试。
以上是关于替换Google Drive UpdateMediaUpload中的文件的主要内容,如果未能解决你的问题,请参考以下文章
通过Google Drive REST API上传或创建包含内容的新文件?
不推荐使用Drive.DriveApi.getAppFolder(mGoogleApiClient)
在google drive使用google.colab云服务