示例#1
0
 public async Task DeleteAllFiles(string fileId)
 {
     using (var client = DriveClient.GetInfo())
     {
         FilesResource.ListRequest listRequest = client.Files.List();
         listRequest.Q       = "'" + fileId + "' in parents and trashed = false";
         listRequest.OrderBy = "name";
         //listRequest.Fields = "nextPageToken, files" +
         //    "(id, name, mimeType, permissions, webContentLink, description, size, shared, owners, parents)";
         Google.Apis.Drive.v3.Data.FileList ListResponse = listRequest.Execute();
         while (ListResponse.Files != null)
         {
             List <Task> tasks = new List <Task>();
             foreach (var item in ListResponse.Files)
             {
                 tasks.Add(Task.Run(() => client.Files.Delete(item.Id).Execute()));
             }
             Task.WaitAll(tasks.ToArray());
             if (ListResponse.NextPageToken == null)
             {
                 break;
             }
             listRequest.PageToken = ListResponse.NextPageToken;
             ListResponse          = listRequest.Execute();
         }
     }
 }
示例#2
0
 public async Task ListFilesAsync(Containers.Directory directory)
 {
     using (DriveService service = DriveClient.GetInfo())
     {
         try
         {
             FilesResource.ListRequest listRequest = service.Files.List();
             listRequest.Q       = "'" + directory.id + "' in parents and trashed = false";
             listRequest.OrderBy = "name";
             listRequest.Fields  = "nextPageToken, files" +
                                   "(id, name, mimeType, permissions, webContentLink, description, size, shared, owners, parents)";
             Google.Apis.Drive.v3.Data.FileList ListResponse = listRequest.Execute();
             while (ListResponse.Files != null && ListResponse.Files.Count != 0)
             {
                 List <Task> tasks = new List <Task>();
                 foreach (var item in ListResponse.Files)
                 {
                     tasks.Add(Task.Run(() => AddToParentDirectoryObject(item, directory)));
                 }
                 Task.WaitAll(tasks.ToArray());
                 if (ListResponse.NextPageToken == null)
                 {
                     break;
                 }
                 listRequest.PageToken = ListResponse.NextPageToken;
                 ListResponse          = listRequest.Execute();
             }
         }
         catch (Exception e)
         {
             throw new Exception(e.StackTrace);
         }
     }
 }
示例#3
0
        public async Task ResumableChunkUpload(string filePath)
        {
            using (var client = DriveClient.GetInfo())
            {
                try
                {
                    FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    FilesResource.CreateMediaUpload request = new FilesResource.CreateMediaUpload(client,
                                                                                                  new Google.Apis.Drive.v3.Data.File
                    {
                        Name    = "new File",
                        Parents = new List <string>()
                        {
                            "1K46j8qE2sOiFAMNvSz21xFxgpVTYlcWm"
                        },
                    },
                                                                                                  stream,
                                                                                                  "video/mpeg4");
                    request.ChunkSize = 1024 * 1024;
                    await request.InitiateSessionAsync();

                    Task <Google.Apis.Upload.IUploadProgress> uploadTask = request.UploadAsync();
                    for (var i = 0; i < 1000; i++)
                    {
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine(request.GetProgress().BytesSent);
                    }
                    await uploadTask;
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message + " " + e.StackTrace);
                }
            }
        }
示例#4
0
 public long GetUsedSpace()
 {
     using (var client = DriveClient.GetInfo())
     {
         Google.Apis.Drive.v3.AboutResource.GetRequest request = client.About.Get();
         request.Fields = "storageQuota";
         var response = request.Execute();
         return((long)response.StorageQuota.UsageInDrive);
     }
 }
示例#5
0
 public MemoryStream Download(Containers.File item)
 {
     using (var client = DriveClient.GetInfo())
     {
         MemoryStream stream          = new MemoryStream();
         var          downloadRequest = client.Files.Get(item.id);
         Task         download        = Task.Run(() => downloadRequest.DownloadAsync(stream));
         download.Wait();
         return(stream);
     }
 }
示例#6
0
 public void createTestFolders()
 {
     using (var client = DriveClient.GetInfo())
     {
         for (var i = 0; i < 10000; i++)
         {
             var fileMetadata = new Google.Apis.Drive.v3.Data.File
             {
                 Name     = "abc" + i.ToString(),
                 MimeType = "application/vnd.google-apps.folder"
             };
             var request = client.Files.Create(fileMetadata);
             request.Execute();
         }
     }
 }
示例#7
0
        public void UploadFile(Containers.File item, MemoryStream stream = null)
        {
            var fileMetadata = new Google.Apis.Drive.v3.Data.File()
            {
                Name    = item.Name,
                Parents = item.Parent,
            };

            FilesResource.CreateMediaUpload request;
            stream.Position = 0;
            using (var service = DriveClient.GetInfo())
            {
                request = service.Files.Create(
                    fileMetadata, stream, item.mimeType);
                request.Upload();
            }
            var file = request.ResponseBody;
        }
示例#8
0
 public void UploadDirectory(Containers.Directory item)
 {
     try
     {
         using (var service = DriveClient.GetInfo())
         {
             var fileMetadata = new Google.Apis.Drive.v3.Data.File()
             {
                 Name     = item.Name,
                 MimeType = "application/vnd.google-apps.folder",
                 Parents  = item.Parent,
             };
             var request = service.Files.Create(fileMetadata).Execute();
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }