从Google云端硬盘自动将文档下载到FTP中

时间:2016-12-08 12:47:41

标签: c# ftp google-drive-api

我正在尝试使用C#通过提供文件名从谷歌驱动器下载特定文档到FTP服务器,文件正在下载没有数据,即零字节大小,而我正在使用代码将文件上传到FTP本地磁盘,但它的工作文件。这是我的代码:

namespace DriveQuickstart
{
    class Program
    {
        static string[] Scopes = { DriveService.Scope.Drive };
        static string ApplicationName = "Drive API .NET Quickstart";
        string lines = "";
        public void UploadFileToFTP(StreamReader source)
        {
            try
            {
                Console.WriteLine(lines);

                String ftpurl = @"ftp://ip_address/"+lines;

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl);           
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential("username", "password");
                request.ContentLength = fileContents.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
                response.Close();
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }

        static void Main(string[] args)
        {
            Program pr = new Program();
            UserCredential credential;
            using (var stream1 = new FileStream(@"C:\Users\Documents\Visual Studio 2015\Projects\DocsToFTP\DocsToFTP\client_secret.json", FileMode.Open, FileAccess.Read))
            {

                string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream1).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 100;
            listRequest.Fields = "nextPageToken, files(id, name)";

            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;

            var driveService1 = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            var fileName = "";
            var fileId = "";      
            Console.WriteLine("Enter name of file to download");
            pr.lines = Console.ReadLine();

            foreach (var file in files)
            {
                Console.WriteLine("{0} ({1})", file.Name, file.Id);
                fileName = file.Name;
                fileId = file.Id;
                var request1 = driveService1.Files.Get(fileId);
                var stream1 = new System.IO.MemoryStream();
                request1.Download(stream1);

                if (pr.lines.Equals(file.Name))
                {
                    FileStream fsw = new FileStream(@"C:\GoogleDrive1\" + file.Name, FileMode.Create);                 
                    stream1.WriteTo(fsw);                    
                    var sr = new StreamReader(fsw);                  
                    pr.UploadFileToFTP(sr);                  
                    fsw.Close();
                    stream1.Close();
                    Console.WriteLine(file.Name + "file downloaded");
                    break;
                }
            }
        }
    }
}

0 个答案:

没有答案