Dart: upload .jpg file to Google Drive

784 views
Skip to first unread message

IMozes

unread,
May 14, 2019, 2:32:06 AM5/14/19
to Flutter Dev
Hi,
I am trying to upload some pictures taken in my application to Google Drive.

I created a project and a Service account key  and I have Google Drive API enabled.

My code snippet looks like this:

  ...

   // sfilePath = "/storage/emulated/0/Xyz";
   // sfileName = "PIC_17";

  final accountCredentials = new ServiceAccountCredentials.fromJson({
    "private_key_id": "...",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "...@...gserviceaccount.com",
    "client_id": "...apps.googleusercontent.com",
    "type": "service_account"
  });

  AuthClient client = await clientViaServiceAccount(
      accountCredentials,

   var driveApi = ga.DriveApi(client);
  
   var fileToUpload = File(sfilePath + sfileName + '.jpg');

   var response = await driveApi.files.create(ga.File()..name = 'my_file.jpg',
         uploadMedia: ga.Media(fileToUpload.openRead(), fileToUpload.lengthSync()));

   print(response.toJson());  

   ...

After running the program print(response.toJson()) results in: {id: ..., kind: drive#file, mimeType: image/jpeg, name: my_file.jpg} 
and I see the Requests number for Google Drive API on Dashboard increasing after a while,  but I can't find my_file.jpg on my Google Drive.

Thanks for any suggestions,
IMozes






Tobias Schiek

unread,
Aug 11, 2020, 6:53:04 AM8/11/20
to Flutter Development (flutter-dev)
Is there a solution to get the progress for uploading a file with "driveApi.files.create" ?
Reply all
Reply to author
Forward
0 new messages