/// <summary> /// Create a new Directory. /// Documentation: https://developers.google.com/drive/v2/reference/files/insert /// </summary> /// <param name="service">a Valid authenticated DriveService</param> /// <param name="title">The title of the file. Used to identify file or folder name.</param> /// <param name="description">A short description of the file.</param> /// <param name="parent"></param> /// <returns> /// Collection of parent folders which contain this file. /// Setting this field will put the file in all of the provided folders. root folder. /// </returns> public static File CreateDirectory(DriveService service, string title, string description, string parent = "root") { File NewDirectory = null; // Create metaData for a new Directory File body = new File(); body.Name = title; body.Description = description; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List <string>() { parent }; try { FilesResource.CreateRequest request = service.Files.Create(body); NewDirectory = request.Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(NewDirectory); }
public static File createDirectory(DriveService _service, string _name, string _description, string _parent) { File NewDirectory = null; // Create metaData for a new Directory var fileMetadata = new File() { Name = _name, // Parents = new List<string>() { _parent }, MimeType = "application/vnd.google-apps.folder", // Description = _description }; try { FilesResource.CreateRequest request = _service.Files.Create(fileMetadata); request.Fields = "id"; NewDirectory = request.Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(NewDirectory); }
public static File createDirectory(DriveService _service, string _title, string _description, string _parent) { var watchi = System.Diagnostics.Stopwatch.StartNew(); File NewDirectory = null; File body = new File(); body.Name = _title; body.Description = _description; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List <string>() { _parent }; try { FilesResource.CreateRequest request = _service.Files.Create(body); NewDirectory = request.Execute(); } catch (Exception e) { MessageBox.Show(e.Message, "Error Occured1"); } watchi.Stop(); var elapsedMs = watchi.ElapsedMilliseconds; Console.WriteLine("time create dir = " + elapsedMs / 1000 + " sec " + elapsedMs % 1000 + "msec"); return(NewDirectory); }
private File CreateDirectory(string name, string description, string parent) { File NewDirectory = null; logger.LogInformation($"New google drive directory creation ({name})"); // Create metaData for a new Directory File file = new File() { Name = name, Description = description, MimeType = "application/vnd.google-apps.folder" }; try { FilesResource.CreateRequest request = Connector.Service.Files.Create(file); NewDirectory = request.Execute(); } catch (Exception e) { logger.LogError("An error occurred: " + e.Message); } return(NewDirectory); }
public static string CreateFolder(string folderName) { try { UserCredential credential = GetCredentials(); DriveService service = new DriveService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = WebConfigurationManager.AppSettings["GoogleDriveApplicationName"] }); Google.Apis.Drive.v3.Data.File fileMetaData = new Google.Apis.Drive.v3.Data.File { Name = folderName, MimeType = "application/vnd.google-apps.folder" }; FilesResource.CreateRequest request = service.Files.Create(fileMetaData); request.Fields = "id"; Google.Apis.Drive.v3.Data.File file = request.Execute(); return(file.Id); } catch (Exception exception) { LogException(exception, new Dictionary <string, string>(), ErrorSource.ServiceHelper); return(String.Empty); } }
// DriveService _service: Valid, authenticated Drive service // string_ title: Title of the folder // string _description: Description of the folder // _parent: ID of the parent directory to which the folder should be created public static File CreateDirectory(DriveService _service, string _foldername, string _folderdescription, string _parent) { File NewDirectory = null; try { File body = new File { Name = _foldername, Description = _folderdescription, MimeType = "application/vnd.google-apps.folder", Permissions = new List <Permission>(), //body.Permissions = new List<Permission> { new Permission(). }; //Parent folder or pass 'root' to create on root Parents = new List <string>() { _parent } }; FilesResource.CreateRequest request = _service.Files.Create(body); NewDirectory = request.Execute(); InsertPermission(_service, NewDirectory); } catch (Exception ex) { throw ex; } return(NewDirectory); }
/// <summary> /// Create folder method. /// </summary> /// <param name="parent">The parent of the folder.</param> /// <param name="folderName">The folder name.</param> /// <returns>A file object of the folder.</returns> public Google.Apis.Drive.v3.Data.File CreateFolder( string parent, string folderName) { Google.Apis.Drive.v3.Data.File fileMetadata = new (); fileMetadata.Name = folderName; fileMetadata.MimeType = "application/vnd.google-apps.folder"; IList <string> parents = new List <string>(); parents.Add(parent); fileMetadata.Parents = parents; FilesResource.CreateRequest request = driveService.Files.Create(fileMetadata); request.Fields = "id, name, parents"; Google.Apis.Drive.v3.Data.File file = request.Execute(); string message = "Folder ID: " + file.Id; Log.Info(CultureInfo.InvariantCulture, m => m( message)); return(file); }
/// <summary> /// Creates a folder /// </summary> /// <param name="_title">name to create it</param> /// <param name="_description">description</param> /// <param name="_parent">parent id, empty for root</param> /// <returns></returns> public static Google.Apis.Drive.v3.Data.File createDirectory(DriveService service, string _title, string _parentID) { Google.Apis.Drive.v3.Data.File NewDirectory = null; // Create metaData for a new Directory Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); body.Name = _title; body.Description = ""; body.MimeType = "application/vnd.google-apps.folder"; List <string> parents = new List <string>(); if (!_parentID.Equals(string.Empty)) { parents.Add(_parentID); } body.Parents = parents; try { FilesResource.CreateRequest request = service.Files.Create(body); NewDirectory = request.Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(NewDirectory); }
/// <summary> /// Create a new Directory file on Google Drive: GoogleDriveService.Files.Create(metaData) /// </summary> /// a Valid authenticated DriveService /// The title of the file. Used to identify file or folder name. /// A short description of the file. /// Collection of parent folders which contain this file. /// Setting this field will put the file in all of the provided folders. root folder. /// Documentation: https://developers.google.com/drive/v3/reference/files/create public GoogleDriveManagerResult CreateFolder(DriveService service, string dirName, string description, string parent) { result = new GoogleDriveManagerResult(); try { Google.Apis.Drive.v3.Data.File folder = null; Google.Apis.Drive.v3.Data.File metaData = new Google.Apis.Drive.v3.Data.File() { Name = dirName, Description = description, MimeType = GoogleDriveMimeService.FolderMimeType }; FilesResource.CreateRequest request = service.Files.Create(metaData); folder = request.Execute(); if (folder != null) { result.GoogleDriveFileId = folder.Id; } else { result.Errors.Add(GoogleDriveManagementConstants.FolderNotCreated); } return(result); } catch (Exception e) { WriteToConsole(GoogleDriveManagementConstants.FolderNotCreatedException + e.Message); result.Exceptions.Add(e); return(result); } }
public AttemptResult CreateFolder(string folder_name, string parent_id, out string id) { FilesResource.CreateRequest request = drive_service.Files.Create( new Google.Apis.Drive.v3.Data.File() { Name = folder_name, MimeType = FOLDER_MIME_TYPE, Parents = new string[] { parent_id } } ); request.Fields = "id"; try { id = request.Execute().Id; } catch (Google.GoogleApiException) { id = null; return(AttemptResult.Failed); } return(AttemptResult.Succeeded); }
private string CreateFolder(GDriveFile objGDriveFile) { string newFileId = null; Google.Apis.Drive.v3.Data.File newDirectory = null; // Create metaData for a new Directory Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File { Name = objGDriveFile.Title, Description = objGDriveFile.Description, MimeType = objGDriveFile.MimeType, Parents = new List <string>() { objGDriveFile.Parent }, }; using (DriveService service = this.AuthenticateServiceAccount()) { FilesResource.CreateRequest request = service.Files.Create(body); request.Fields = "*"; newDirectory = request.Execute(); newFileId = newDirectory.Id; } return(newFileId); }
public static string createFolder(DriveService _service, string _title, string _parent, int id) { Google.Apis.Drive.v3.Data.File NewDirectory = null; // Create metaData for a new Directory Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); body.Name = _title; body.Description = ""; body.MimeType = "application/vnd.google-apps.folder"; //body.Parents = "root"; if (!string.IsNullOrEmpty(_parent)) { body.Parents = new List <string>() { _parent }; } ; //create parent folder for app try { FilesResource.CreateRequest request = _service.Files.Create(body); NewDirectory = request.Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(NewDirectory.Id); }
public string createDirectory() { UserCredential credential; using (var stream = new FileStream(Server.MapPath("~/Documents/credentials.json"), FileMode.Open, FileAccess.ReadWrite)) { // The file token.json stores the user's access and refresh tokens, and is created // automatically when the authorization flow completes for the first time. string credPath = Server.MapPath(@"~\Documents\token.json"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "admin", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); string _parent = "AppFolder"; // Create metaData for a new Directory Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); body.Name = "eOffice"; body.Description = ""; body.MimeType = "application/vnd.google-apps.folder"; //body.Parents = new List<string>() { "root" }; if (!string.IsNullOrEmpty(_parent)) { body.Parents = new List <string>() { "root" }; } ; //create parent folder for app // public static DriveService serv2 = new DriveService(); try { //Google.Apis.Drive.v3.FilesResource.ListRequest checkDirectory = _service.Files.List(); FilesResource.CreateRequest request = service.Files.Create(body); Google.Apis.Drive.v3.Data.File NewDirectory = request.Execute(); return(NewDirectory.Id); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); return(""); } }
public void CreateApplicationFolder() { var fileMetadata = new File { Name = SpotbaseFolder, MimeType = "application/vnd.google-apps.folder" }; FilesResource.CreateRequest request = _driveService.Files.Create(fileMetadata); request.Fields = "id"; request.Execute(); }
// https://developers.google.com/drive/api/v3/folder private string CreateFolder(string parentId, string name) { Google.Apis.Drive.v3.Data.File folderToCreate = new Google.Apis.Drive.v3.Data.File(); folderToCreate.MimeType = Constants.GOOGLE_DRIVE_MIMETYPE_FOLDER; folderToCreate.Name = name; FilesResource.CreateRequest request = Service.Files.Create(folderToCreate); request.Fields = "id"; Google.Apis.Drive.v3.Data.File created = request.Execute(); return(created.Id); }
public static async Task <IUploadProgress> UploadFile(string uploadFile, Dictionary <string, string> appFileAttributes = null) { IsUploadingFile = true; DriveService service = GetGDriveService(); IUploadProgress progress = null; //Task<IUploadProgress> progress = null; if (System.IO.File.Exists(uploadFile)) { File body = new File(); body.Name = Path.GetFileName(uploadFile); _uploadFilename = body.Name; body.Description = FileUploadDescription; body.Properties = appFileAttributes; body.MimeType = GetMimeType(uploadFile); //body.Id = DateTime.Now.ToString("dd-HH:mm:ss.fffff"); try { // File's content. byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile); MemoryStream stream = new MemoryStream(byteArray); //FilesResource.UpdateMediaUpload uploadRequest = service.Files.Update(f, "", stream, body.MimeType); FilesResource.CreateRequest cr = service.Files.Create(body); File f = cr.Execute(); //FilesResource.CreateMediaUpload uploadRequest = service.Files.Create(f, stream, body.MimeType); //uploadRequest.Body.Id = DateTime.Now.ToString("dd-HH:mm:ss.fffff"); //body.Id = "WRJskjslkdjslj3q9"; FilesResource.CreateMediaUpload uploadRequest = service.Files.Create(body, stream, body.MimeType); //FilesResource.CreateMediaUpload ur = new FilesResource.CreateMediaUpload(service, body, stream, body.MimeType); //uploadRequest.Fields = "Id"; //uploadRequest.Body.Properties = new Dictionary<string, string>(); //uploadRequest.Body.Properties.Add("sdkjfs", "kdjskjd"); uploadRequest.SupportsTeamDrives = true; uploadRequest.ResponseReceived += UploadRequest_ResponseReceived; uploadRequest.ProgressChanged += UploadRequest_ProgressChanged; // new Exception("Exception"); progress = await uploadRequest.UploadAsync();//.UploadAsync(); } catch (Exception e) { Debug.WriteLine("An error occurred: " + e.Message); throw; } finally { IsUploadingFile = false; } } service?.Dispose(); return(progress);// uploadedFile == null ? string.Empty : uploadedFile.Id; }
public GoogleDriveFileModel CreateFolder(string folderName, List <string> parents) { File fileMetadata = new File() { Name = folderName, MimeType = GoogleDriveFileTypeEnum.Folder.GetDescription() }; fileMetadata.Parents = parents; FilesResource.CreateRequest request = this._driveService.Files.Create(fileMetadata); File file = request.Execute(); return(Mapper.MapFileToGoogleDriveFileModel(file)); }
public string CreateGDriveDir(string NameDir) { var file = new Google.Apis.Drive.v3.Data.File() { Name = NameDir, MimeType = "application/vnd.google-apps.folder" }; FilesResource.CreateRequest req = Service.Files.Create(file); req.Fields = "id"; file = req.Execute(); return(file.Id); }
private static DriveFile CreateFolder(DriveService driveService, string targetFolderId, DriveFile sourceFile, string folderMimeType) { DriveFile targetFile = new DriveFile { MimeType = folderMimeType, Name = sourceFile.Name, Parents = new[] { targetFolderId }, }; FilesResource.CreateRequest createRequest = driveService.Files.Create(targetFile); createRequest.SupportsAllDrives = true; targetFile = createRequest.Execute(); return(targetFile); }
/// <summary> /// Метод для подключения к Google Drive и создания нового Google Sheet /// </summary> /// <returns></returns> public Google.Apis.Drive.v3.Data.File CreateSheet() { string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile, }; var clientId = ConfigurationManager.AppSettings["clientId"];; var clientSecret = ConfigurationManager.AppSettings["clientSecret"]; // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("MyAppsToken")).Result; //Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent. DriveService _service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "MyAppName", }); var _parent = "1Ha0GYN6r9_KWRVgN0PHOKpduvU83-2FK";//ID of folder if you want to create spreadsheet in specific folder var filename = "helloworld"; var fileMetadata = new Google.Apis.Drive.v3.Data.File() { Name = filename, MimeType = "application/vnd.google-apps.spreadsheet", //TeamDriveId = teamDriveID, // IF you want to add to specific team drive }; FilesResource.CreateRequest request = _service.Files.Create(fileMetadata); request.SupportsTeamDrives = true; fileMetadata.Parents = new List <string> { _parent }; // Parent folder id or TeamDriveID request.Fields = "id"; System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; var file = request.Execute(); Console.WriteLine("File ID: " + file.Id); Configuration configManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); KeyValueConfigurationCollection confCollection = configManager.AppSettings.Settings; confCollection["googleId"].Value = file.Id; configManager.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name); return(file); }
/// <summary> /// 建立一個資料夾 /// </summary> /// <param name="_service"></param> /// <param name="parentid"></param> /// <param name="title"></param> /// <param name="description"></param> /// <returns></returns> public static GData.File CreateFolder(DriveService _service, string parentid, string title, string description = "") { try { // setting file string mimeType = "application/vnd.google-apps.folder"; GData.File body = GenFileObject(parentid, title, description, mimeType); FilesResource.CreateRequest request = _service.Files.Create(body); GData.File folder = request.Execute(); return(folder); } catch { throw; } }
internal string CreateFolder(string name, string parentId) { var body = new File { Name = name, MimeType = FolderType, Parents = new List <string> { parentId } }; FilesResource.CreateRequest createRequest = _driveService.Files.Create(body); File result = createRequest.Execute(); return(result.Id); }
/// Create a new Directory. /// Documentation: https://developers.google.com/drive/v2/reference/files/insert /// /// a Valid authenticated DriveService /// The title of the file. Used to identify file or folder name. /// A short description of the file. /// Collection of parent folders which contain this file. /// Setting this field will put the file in all of the provided folders. root folder. /// public static File createDirectory(DriveService _service, string _title, string _description, string _parent) { File NewDirectory = null; // Init the parentId string parentId = "root"; // Get the file id of the parent Folder, in case it is not "root" if (_parent.ToUpper() != "ROOT") { var parentList = GetFilesByName(_service, _parent, NameSearchOperators.Is); // it's possible, that folder name was given more than once -> for this time we will handle the // request just in case we find a unique folder name, otherwise we are not sure, which folder we found. if (parentList.Count() != 1) { throw new ArgumentException($"Parent {_parent} not found", "parent"); } parentId = parentList.FirstOrDefault().Id; } // in case parentId is still null, we will create our new folder in root folder // Create metaData for a new Directory File body = new File(); body.Name = _title; body.Description = _description; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List <string> { parentId }; try { FilesResource.CreateRequest request = _service.Files.Create(body); NewDirectory = request.Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(NewDirectory); }
public string CreateFolder(string folderName, string parentFolderId) { var folder = new File() { Name = folderName, MimeType = "application/vnd.google-apps.folder", Parents = new List <string>() { parentFolderId } }; FilesResource.CreateRequest request = service.Files.Create(folder); var newFolder = request.Execute(); return(newFolder.Id); }
/// <summary> /// The folders are created on Google Drive, which do not exist. /// </summary> /// <param name="gDPath"></param> /// <returns>The last folder id</returns> private string CreateGDPath(string gDPath) { string parentId = ConvertGDFolderToGDParentId(gDPath); if (parentId != null) { return(parentId); } Queue <string> folders = new Queue <string>(gDPath.Split('\\')); folders.Dequeue(); parentId = folderIds["root"]; string path = ""; while (folders.Count != 0) { string folder = folders.Dequeue(); path += "\\" + folder; string newParentId = ConvertGDFolderToGDParentId(path); if (newParentId == null) { Google.Apis.Drive.v3.Data.File gDFolder = new Google.Apis.Drive.v3.Data.File(); gDFolder.Name = folder; gDFolder.MimeType = "application/vnd.google-apps.folder"; gDFolder.Parents = new List <string>(); gDFolder.Parents.Add(parentId); FilesResource.CreateRequest request = Service.Files.Create(gDFolder); request.Fields = "id"; var result = request.Execute(); newParentId = result.Id; folderIds.Add(path, newParentId); } parentId = newParentId; } return(parentId); }
public File CreateDirectory(string title, string description, string parent) { // Create metaData for a new Directory File body = new File(); body.Name = title; body.Description = description; body.MimeType = "application/vnd.google-apps.folder"; if (!string.IsNullOrEmpty(parent)) { body.Parents = new List <string> { parent }; } FilesResource.CreateRequest request = service.Files.Create(body); request.Fields = "id"; return(request.Execute()); }
public static Task <Models.FileListItem> NewFolder(string parent, string folderName) { return(Task.Run(delegate() { File newFolder = new File() { Name = folderName, MimeType = Constants.MIME_GDRIVE_DIRECTORY, Parents = new List <string>() { parent } }; FilesResource.CreateRequest createRequest = Service.Files.Create(newFolder); createRequest.Fields = "id, name, ownedByMe, modifiedTime, size, mimeType"; return Misc.FileToItem(createRequest.Execute()); })); }
private File CreateNoteDirectory() { // Create metaData for a new Directory File body = new File(); body.Name = _appName; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List <string>() { "root" }; try { FilesResource.CreateRequest request = _service.Files.Create(body); return(request.Execute()); } catch (Exception e) { throw e; } }
/// <summary> /// Создание директории /// </summary> /// <param name="service">Сервис</param> /// <param name="directoryName">Название директории</param> /// <param name="parentDirectoryId">Идентификатор родительской директории</param> /// <returns></returns> public File CreateDirectory(DriveService service, string directoryName, string parentDirectoryId = null) { File body = new File { Name = directoryName, Description = "Created by ScanImage", MimeType = DirectoryMimeType }; if (!string.IsNullOrEmpty(parentDirectoryId)) { body.Parents = new List <string> { parentDirectoryId } } ; FilesResource.CreateRequest request = service.Files.Create(body); File directory = request.Execute(); return(directory); }
private GoogleFile CreateFile(string type, string name, string fields, string parent = null, string description = null) { var file = new GoogleFile { Name = name, MimeType = type, Description = description, }; if (parent != null) { file.Parents = new List <string> { parent }; } FilesResource.CreateRequest request = DriveService.Files.Create(file); request.Fields = fields; return(request.Execute()); }