C# Google驱动器API未从IIS上载文件

C# Google驱动器API未从IIS上载文件,c#,iis,google-drive-api,google-api-dotnet-client,service-accounts,C#,Iis,Google Drive Api,Google Api Dotnet Client,Service Accounts,我使用GoogleAPI.Net客户端库通过GoogleDrive API使用服务帐户上传到GoogleDrive。当我尝试从VisualStudio(调试)进行调试时,或者甚至当我在本地IIS上部署它时,这都能很好地工作 但当我将文件部署到我的服务器(MicrosoftServer2012,IIS8.5)上时,文件不会被上传,也不会引发任何异常。下面是一段代码: byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile); Logger

我使用GoogleAPI.Net客户端库通过GoogleDrive API使用服务帐户上传到GoogleDrive。当我尝试从VisualStudio(调试)进行调试时,或者甚至当我在本地IIS上部署它时,这都能很好地工作

但当我将文件部署到我的服务器(MicrosoftServer2012,IIS8.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;
我得到的回报是零。上面的代码在try块中,catch正在记录异常,但没有抛出异常

我已授予IIS用户对此文件夹的完全访问权限

有人面临过同样的问题吗?欢迎任何指向解决方案的指针

更新

它适用于除Office文件以外的所有文件。由于XLSX等无法在google drive上正常运行,我修改了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; 

我不确定这是否是问题所在。我没有生产IIS服务器,无法对其进行测试。我怀疑问题可能与mime类型有关,我不确定它如何在本地系统上工作,而不是在您的生产系统上,但请尝试以下代码。如果不起作用,我可以删除答案

确保您添加了

request.Convert = true;
这告诉驱动器将文件转换为驱动器格式,而不仅仅是上传xls文件

代码

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;
            }           

        }
私有静态字符串GetMimeType(字符串文件名)
{
字符串mimeType=“应用程序/未知”;
字符串ext=System.IO.Path.GetExtension(文件名).ToLower();
Microsoft.Win32.RegistryKey regKey=Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if(regKey!=null&®Key.GetValue(“内容类型”)!=null)
mimeType=regKey.GetValue(“内容类型”).ToString();
返回mimeType;
}
/// 
///上载文件
///文件:https://developers.google.com/drive/v2/reference/files/insert
/// 
///有效的经过身份验证的驱动器服务
///要上载的文件的路径
///包含此文件的父文件夹的集合。
///设置此字段将把文件放在所有提供的文件夹中。根文件夹。
///如果上载成功,则返回上载文件的文件资源
///如果上载失败,则返回null
公共静态文件上传文件(DriveService\u服务、字符串\u上传文件、字符串\u父级){
if(System.IO.File.Exists(_uploadFile))
{
文件体=新文件();
body.Title=System.IO.Path.GetFileName(_uploadFile);
body.Description=“Diamto驱动样本上传的文件”;
body.MimeType=GetMimeType(_uploadFile);
body.Parents=new List(){new ParentReference(){Id=\u parent}};
//文件的内容。
byte[]byteArray=System.IO.File.ReadAllBytes(\u uploadFile);
System.IO.MemoryStream stream=新的System.IO.MemoryStream(byteArray);
尝试
{
fileResource.InsertMediaUpload请求=_service.Files.Insert(body、stream、GetMimeType(_uploadFile));
request.Convert=true;
request.Upload();
返回request.ResponseBody;
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
返回null;
}
}
否则{
WriteLine(“文件不存在:”+\u uploadFile);
返回null;
}           
}

从我刚刚添加的转换中删除的代码。

您是否100%确定IIS可以访问包含您试图上载的文件的目录?试着在上面打开一个文件或者做一些事情来测试它是否有访问权限。是的@DalmTo我非常确定它有访问权限,即使是尝试,我已经给了“所有人”完全访问权限。你能做一个files.list或者其他什么吗?只是想检查一下,你正在访问谷歌,它不是一个防火墙阻止身份验证服务器的问题。取得了一个进展,它的PDF,TXT等工作,但只是不工作的docx,pptx和xlsx。我已经更新了问题。的所有链接都在书签中。我从你的帖子中学到了所有的谷歌api代码。请看一下GetMimeType,我只修改了查看上传XLSX、PPTX等。有没有办法解决这个问题?还是不走运,我认为docx等是ZIP.check硬编码解决方案。但那太乱了。为什么您的服务器返回错误的mime类型。。。。(思考)我认为我们的服务器上没有word或excel,因此它在注册表中不可用。正如这里所说,这是有道理的。尝试更改您的代码,以添加正确的mime类型的文件,而不是谷歌的mime类型。我很高兴我们得到了它的工作。我想我将编辑这个示例项目并编写一些硬代码。这个问题可能会影响其他问题。
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;
            }           

        }