Reputation: 16772
The example Drive API v3 code shows that to save a file to Google drive from your app you have to create a local file on disk first, then upload it.
Since I'm using Flutter for web (as well as Windows and Android) I can't save files to disk. (Nor would I want to, as it's very slow compared to just sending bytes in memory over http.)
How can I send data in memory straight to Google and have it saved as a file please (and vice versa)? I can't find any Dart code for this, nor Drive examples, anywhere that I've Googled.
Upvotes: 3
Views: 1369
Reputation: 4750
Use
dependencies:
google_drive_client: ^1.0.3
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:example/config.dart';
import 'package:flutter/material.dart';
import 'package:google_drive_client/google_drive_client.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
final GoogleDriveClient client = GoogleDriveClient(Dio(), getAccessToken: () async => Config.ACCESS_TOKEN);
final String id = '';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListView(
children: [
FlatButton(
child: Text('list'),
onPressed: () async {
print(await client.list());
},
),
FlatButton(
child: Text('create'),
onPressed: () async {
final File file = File((await getTemporaryDirectory()).path + '/testing2');
file.writeAsStringSync("contents");
var meta = GoogleDriveFileUploadMetaData(name: 'testing');
print(await client.create(meta, file));
},
),
FlatButton(
child: Text('delete'),
onPressed: () async {
await client.delete(id);
},
),
FlatButton(
child: Text('download'),
onPressed: () async {
await client.download(id, 'testing');
},
),
FlatButton(
child: Text('get'),
onPressed: () async {
print(await client.get(id));
},
),
],
),
),
);
}
}
Upvotes: 0
Reputation: 117016
I know your question is about flutter. I am not a flutter dev. After discussing this with you in comments it sounds like you are looking to find out if its even possible.
I went a head and tested this with C# and i can tell you that it is possible. All you have to do is turn your text into a memory stream and then upload that instead of a file stream. As it sounds like you already have the text in a memory stream you should be able to just upload it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Upload;
namespace ConsoleApp1
{
class Program
{
static readonly string CredentialsFile = "ServiceAccountCreds.json";
private static readonly string FileName = "UploadFileString.txt";
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var serviceAccountCredentials = GoogleCredential.FromFile(CredentialsFile)
.CreateScoped(DriveService.ScopeConstants.Drive);
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = serviceAccountCredentials,
ApplicationName = "Discovery Authentication Sample",
});
var uploadString = "Test";
// Upload file Metadata
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = FileName,
Parents = new List<string>() {"1R_QjyKyvET838G6loFSRu27C-3ASMJJa"}
};
// Convert raw text to memory stream for upload.
var fsSource = new MemoryStream(Encoding.UTF8.GetBytes(uploadString ?? ""));
string uploadedFileId;
// // Create a new file on Google Drive
// await using (var fsSource = new FileStream(FileName, FileMode.Open, FileAccess.Read))
// await using (var fsSource = new FileStream(FileName, FileMode.Open, FileAccess.Read))
// {
// Create a new file, with metadata and stream.
var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
request.Fields = "*";
var results = await request.UploadAsync(CancellationToken.None);
if (results.Status == UploadStatus.Failed)
{
Console.WriteLine($"Error uploading file: {results.Exception.Message}");
}
// the file id of the new file we created
uploadedFileId = request.ResponseBody?.Id;
//}
}
}
}
In theory any of the Flutter samples should work.
Upvotes: 2