Exemple #1
0
 public GoogleDriveUploadStatus(string uri, CreateMediaUpload mediaUpload)
 {
     Id           = uri;
     _mediaUpload = mediaUpload;
     _progress    = _mediaUpload.GetProgress();
     Status       = _progress.Status.ToString();
 }
        private static void Upload2GoogleDrive(UserCredential credential, FileStream uploadStream, string fileName, string path, string applicationName, string mimeType)
        {
            // Create the service using the client credentials.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = applicationName
            });

            string parentId = "root";

            if (path != string.Empty)
            {
                string[] folders = path.Split('/');
                foreach (var folder in folders)
                {
                    var request = service.Files.List();
                    request.Q      = string.Format("'{0}' IN parents and name='{1}' and trashed=false and mimeType='application/vnd.google-apps.folder'", parentId, folder);
                    request.Spaces = "drive";
                    request.Fields = "nextPageToken, files(id, name)";
                    var result = request.Execute();
                    if (result.Files.Count == 0)
                    {
                        var fileMetadata = new Google.Apis.Drive.v3.Data.File();
                        fileMetadata.Name     = folder;
                        fileMetadata.MimeType = "application/vnd.google-apps.folder";
                        fileMetadata.Parents  = new List <string>()
                        {
                            parentId
                        };
                        var createFolderRequest = service.Files.Create(fileMetadata);
                        createFolderRequest.Fields = "id";
                        var file = createFolderRequest.Execute();
                        parentId = file.Id;
                    }
                    else
                    {
                        parentId = result.Files[0].Id;
                    }
                }
            }

            // Get the media upload request object.
            CreateMediaUpload insertRequest = service.Files.Create(
                new Google.Apis.Drive.v3.Data.File
            {
                Name    = fileName,
                Parents = new List <string>()
                {
                    parentId
                }
            },
                uploadStream,
                mimeType);

            // Add handlers which will be notified on progress changes and upload completion.
            // Notification of progress changed will be invoked when the upload was started,
            // on each upload chunk, and on success or failure.
            insertRequest.ProgressChanged  += Upload_ProgressChanged;
            insertRequest.ResponseReceived += Upload_ResponseReceived;

            insertRequest.Upload();
        }