Last active
June 11, 2018 09:35
-
-
Save x213212/340a324c25aee5b8df9513806ced0187 to your computer and use it in GitHub Desktop.
Google_Drive_Api_c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void Create_folder(string folder_name, DriveService service) | |
{ | |
string pageToken = null; | |
var fileMetadata = new Google.Apis.Drive.v3.Data.File() | |
{ | |
Name = folder_name, | |
MimeType = "application/vnd.google-apps.folder" | |
}; | |
var request = service.Files.Create(fileMetadata); | |
request.Fields = "id"; | |
var file = request.Execute(); | |
MessageBox.Show(file.Id); | |
Console.WriteLine("Folder ID: " + file.Id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void Download(string file_name, DriveService service,String savato) | |
{ | |
var fileId = file_name; | |
var request = service.Files.Get(fileId); | |
var stream = new System.IO.MemoryStream(); | |
// Add a handler which will be notified on progress changes. | |
// It will notify on each chunk download and when the | |
// download is completed or failed. | |
request.MediaDownloader.ProgressChanged += | |
(IDownloadProgress progress) => | |
{ | |
switch (progress.Status) | |
{ | |
case DownloadStatus.Downloading: | |
{ | |
Console.WriteLine(progress.BytesDownloaded); | |
break; | |
} | |
case DownloadStatus.Completed: | |
{ | |
Console.WriteLine("Download complete."); | |
MessageBox.Show("Download complete."); | |
SaveStream(stream, savato); | |
break; | |
} | |
case DownloadStatus.Failed: | |
{ | |
Console.WriteLine("Download failed."); | |
MessageBox.Show("Download failed."); | |
break; | |
} | |
} | |
}; | |
request.Download(stream); | |
} | |
private static void SaveStream(System.IO.MemoryStream stream, string saveTo) | |
{ | |
using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write)) | |
{ | |
stream.WriteTo(file); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UserCredential credential; | |
credential = GetCredentials(); | |
// Create Drive API service. | |
var service = new DriveService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = ApplicationName, | |
}); | |
//UploadBasicImage("C:\\0110000010001.txt", service); | |
// MessageBox.Show(Searach("Invoices", service)); | |
// UploadBasicData("C:\\0110000010001.txt", service, Searach("Invoices", service,0)); | |
// Download(Searach("0110000010001.txt", service,1),service, "C:\\qwe.txt"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static UserCredential GetCredentials() | |
{ | |
UserCredential credential; | |
using (var stream = new FileStream("client_id.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(stream).Secrets, | |
Scopes, | |
"user", | |
CancellationToken.None, | |
new FileDataStore(credPath, true)).Result; | |
// Console.WriteLine("Credential file saved to: " + credPath); | |
} | |
return credential; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String Searach(string file_name, DriveService service,int change) | |
{ | |
FilesResource.ListRequest request = service.Files.List(); | |
//find folder | |
if( change==0) | |
request.Q = "mimeType = 'application/vnd.google-apps.folder' and name = '" + file_name + "'"; | |
else | |
//find file | |
request.Q = "mimeType = 'text/plain' and name = '" + file_name + "'"; | |
var folderId = request.Execute(); // Error occurs here upon execution | |
List<string> File_Id = new List<string>(); | |
List<string> File_Name = new List<string>(); | |
// MessageBox.Show(request.Execute().Files.Count.ToString()); | |
for (int i = 0; i < folderId.Files.Count; i++) | |
{ | |
File_Id.Add(folderId.Files[i].Id); | |
File_Name.Add(folderId.Files[i].Name); | |
} | |
String find_id = null; | |
for (int i = 0; i < folderId.Files.Count; i++) | |
{ | |
if (File_Name[0] == file_name) | |
find_id = File_Id[0]; | |
} | |
return find_id; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void UploadBasicData(string path, DriveService service,string foloder_id) | |
{ | |
var folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E"; | |
var fileMetadata = new Google.Apis.Drive.v3.Data.File(); | |
fileMetadata.Name = Path.GetFileName(path); | |
fileMetadata.MimeType = "text/plain"; | |
fileMetadata.Parents = new List<string> { foloder_id }; | |
FilesResource.CreateMediaUpload request; | |
using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open)) | |
{ | |
request = service.Files.Create(fileMetadata, stream, "image/jpeg"); | |
request.Fields = "id"; | |
request.Upload(); | |
} | |
var file = request.ResponseBody; | |
Console.WriteLine("File ID: " + file.Id); | |
MessageBox.Show("ok"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
創建一個雲端資料夾(注意資料夾名稱可以重複但是,id不同)
呼叫範例
Create_folder(“123” , service)
搜尋子資料夾,和檔案
呼叫範例
模式0的話就是搜尋子資料夾,則會回傳尋找到的資料夾 id
Searach("Invoices", service,0)
模式1的話就是搜尋檔案,則會回傳尋找到的資料 id
Searach("0110000010001.txt", service,1)
上傳檔案
呼叫範例
UploadBasicData("C:\0110000010001.txt", service, Searach("Invoices", service));
上傳一個xxxx檔到雲端的Invoices 資料夾
下載檔案
呼叫範例
Download(Searach("0110000010001.txt", service,1),service, "C:\qwe.txt");