【发布时间】:2015-10-13 08:04:18
【问题描述】:
我正在使用 Google APIs .Net 客户端库通过 Google Drive API 使用服务帐户上传到 Google Drive。当我从 Visual Studio(调试)尝试时,这很好用,甚至当我在本地 IIS 上部署它时也能正常工作。
但是当我在我的服务器(Microsoft Server 2012,IIS 8.5)上部署文件时,文件没有上传,也没有抛出任何异常。这是一段代码:
byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
Logger.LoggingService.LogError("GoogleHelper", "Is byteArray null :" + (byteArray == null)); // Line to check if bytearray is null, I am getting false in log.
Logger.LoggingService.LogError("GoogleHelper", "ByteArray length :" + byteArray.Length); // Getting actual length here.
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = DriveService.Files.Insert(body, stream, GetMimeType(uploadFile));
request.Upload();
return request.ResponseBody;
我得到 Null 作为回报。上面的代码在 try 块中,catch 正在记录异常,但没有抛出异常。
我已授予 IIS 用户对此文件夹的完全访问权限。
有人遇到过同样的问题吗?欢迎任何指向解决方案的指针。
更新
它适用于除 Office 文件之外的所有文件。由于 XLSX 等在谷歌驱动器上看起来不正确,我修改了 MIME 类型,如下所示:
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = System.IO.Path.GetFileName(uploadFile);
body.Description = description;
body.MimeType = GetMimeType(uploadFile, false);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = parent } };
byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = DriveService.Files.Insert(body, stream, GetMimeType(uploadFile));
request.ResponseReceived += request_ResponseReceived;
request.Upload();
return request.ResponseBody;
看到我已经调用 GetMimeType 两次 body.MimeType = GetMimeType(uploadFile, false); 和 DriveService.Files.Insert(body, stream, GetMimeType(uploadFile)) 以便文件正确上传到 Google 驱动器,她是我的 GetMimeType 方法:
private string GetMimeType(string fileName, bool ignoreExtension = true)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
if (ignoreExtension == false)
{
switch (ext)
{
case ".ppt":
case ".pptx":
mimeType = "application/vnd.google-apps.presentation";
break;
case ".xls":
case ".xlsx":
mimeType = "application/vnd.google-apps.spreadsheet";
break;
case ".doc":
case ".docx":
mimeType = "application/vnd.google-apps.document";
break;
default:
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
break;
}
}
else
{
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
}
return mimeType;
}
【问题讨论】:
-
您是否 100% 确定 IIS 有权访问包含您尝试上传的文件的目录?尝试在上面打开一个文件或测试它是否可以访问。
-
是的@DalmTo 我非常确定它可以访问,即使尝试一下,我也已授予“每个人”的完全访问权限。
-
你能做一个files.list之类的吗?只是为了检查您是否可以访问 Google,而不是防火墙阻止身份验证服务器问题。
-
取得了进展,它适用于 PDF、TXT 等,但不适用于 docx、pptx 和 xlsx。我已经更新了问题。
-
daimto.com 的所有链接都在书签中。我只从您的帖子中学习了所有 google api 代码。请看一下GetMimeType,我修改后只能正确查看上传XLSX、PPTX等。有什么办法解决吗?
标签: c# iis google-drive-api google-api-dotnet-client service-accounts