Beispiel #1
0
        /// <summary>
        /// Realiza upload de archivos de un path donde ya exista un archivo ubicado en un directorio temporal
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string FileUpload(string path, string fileName, string parentFolder = null)
        {
            try
            {
                var folderId = "";
                //creando carpeta de resultar parentFolder != null
                if (parentFolder != null)
                {
                    FilesResource.ListRequest FileListRequest = service.Files.List();
                    FileListRequest.Fields = "nextPageToken, files(id, name, size, version, trashed, createdTime)";
                    IList <Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;


                    if (files != null && files.Count > 0)
                    {
                        foreach (var file in files)
                        {
                            GoogleDriveFiles File = new GoogleDriveFiles
                            {
                                Id          = file.Id,
                                Name        = file.Name,
                                Size        = file.Size,
                                Version     = file.Version,
                                CreatedTime = file.CreatedTime
                            };

                            if (file.Name == parentFolder && file.Trashed == false)
                            {
                                folderId = file.Id;
                                break;
                            }
                        }
                    }

                    if (folderId == "")
                    {
                        var folderMetadata = new Google.Apis.Drive.v3.Data.File()
                        {
                            Name     = parentFolder,
                            MimeType = "application/vnd.google-apps.folder",
                            Parents  = new List <string>
                            {
                                ConfigurationManager.AppSettings["GDRIVE_SITRAD_FILE_ID"]
                            }
                        };
                        var requestFolder = service.Files.Create(folderMetadata);
                        requestFolder.Fields = "id";
                        var folderRes = requestFolder.Execute();
                        folderId = folderRes.Id;
                    }
                }
                else
                {
                    folderId = ConfigurationManager.AppSettings["GDRIVE_SITRAD_FILE_ID"];
                }

                var FileMetaData = new Google.Apis.Drive.v3.Data.File();
                FileMetaData.Name     = fileName;
                FileMetaData.MimeType = MimeTypes.GetMimeType(fileName);
                FileMetaData.Parents  = new List <string>();
                FileMetaData.Parents.Add(folderId);

                FilesResource.CreateMediaUpload request;
                using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
                {
                    request        = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
                    request.Fields = "id";
                    request.Upload();
                }

                return(request.ResponseBody.Id);
            }
            catch (Exception ex)
            {
                //log.Error(String.Format("Error de comunicación: [{0}: {1}]\r\nStack Trace:\r\n{2}",
                //     ex.Source, ex.Message, ex.StackTrace));
                //if (ex.InnerException != null)
                //{
                //    log.Error(String.Format("Inner Exception: [{0}: {1}]\r\nStack Trace:\r\n{2}",
                //        ex.InnerException.Source, ex.InnerException.Message, ex.InnerException.StackTrace));
                //}
                return("-1");
            }
        }