Google Drive API无法从IIS上传文件 [英] Google Drive API not uploading file from IIS
问题描述
我正在使用Google APIs .Net客户端库通过使用服务帐户的Google Drive API上载到Google Drive.当我从Visual Studio尝试(调试)时,此方法效果很好,甚至当我将其部署到本地IIS上时,效果也很好.
I am using Google APIs .Net client library to upload to Google drive though the Google Drive API using a service account. This works well when I try from Visual Studio (debugging) or even works when I deploy it on my local IIS.
但是,当我在服务器(Microsoft Server 2012,IIS 8.5)上部署文件时,该文件未上载,也不会引发任何异常.这是一段代码:
But file does not get uploaded when I deploy it on my server (Microsoft Server 2012, IIS 8.5), also does not throw any exception. Here is the piece of code:
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记录了异常,但是没有引发异常.
I am getting Null in return. Above code is inside try block and catch is logging the exception, but no exception is thrown.
我已授予IIS用户对该文件夹的完全访问权限.
I have given the full access to IIS user to this folder.
有人遇到过同样的问题吗?任何指向解决方案的指针都欢迎.
Anybody has faced same issue? Any pointer towards solution is welcome.
更新
它适用于Office文件以外的所有文件.由于XLSX等在Google驱动器上无法正常显示,因此我修改了MIME类型,如下所示:
Its working for all files except the Office files. Since XLSX etc were not looking properly on google drive I had modified the MIME type like following:
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:
See I have called GetMimeType twice body.MimeType = GetMimeType(uploadFile, false);
and DriveService.Files.Insert(body, stream, GetMimeType(uploadFile))
so that file get uploaded on Google drive properly and her is my method 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;
}
推荐答案
我不确定这是否是问题.我没有无法测试的生产IIS服务器.我怀疑问题可能出在mime类型上,我不确定它在本地系统(而不是您的生产系统)上如何工作,请尝试使用此代码.如果它不起作用,我可以删除答案.
I am not sure if this is the problem. I don't have a production IIS server I cant test it on. I suspect the issue might be with mime type, I am not sure how it works on local system and not your production system but try this code. If it doesn't work I can delete the answer.
确保添加
request.Convert = true;
这告诉驱动器将文件转换为驱动器格式,而不仅仅是上传xls.
This tells drive to convert the file to drive format and not just upload an xls for example.
代码
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
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;
}
/// <summary>
/// Uploads a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/insert
/// </summary>
/// <param name="_service">a Valid authenticated DriveService</param>
/// <param name="_uploadFile">path to the file to upload</param>
/// <param name="_parent">Collection of parent folders which contain this file.
/// Setting this field will put the file in all of the provided folders. root folder.</param>
/// <returns>If upload succeeded returns the File resource of the uploaded file
/// If the upload fails returns null</returns>
public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {
if (System.IO.File.Exists(_uploadFile))
{
File body = new File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File uploaded by Diamto Drive Sample";
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Convert = true;
request.Upload();
return request.ResponseBody;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
else {
Console.WriteLine("File does not exist: " + _uploadFile);
return null;
}
}
从 查看全文