How to save a file to Google Drive from Flutter web without a local file on disk?
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.
Richard
Updated on January 02, 2023Comments
-
Richard over 1 year
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.
-
DaImTo over 2 yearsTechnically its a file stream that gets uploaded so the data would be encoded to that of the type of file it is. How was this data created if its not from a file? What type of data is it? What about the google docs api or the sheets api if its file data you are trying to upload.
-
Richard over 2 yearsIt's a text file. The data is created in the app by the user. The data will be stored in application settings, hence the link to that page in my question.
-
DaImTo over 2 yearsCorrect me if I'm wrong. Its not a text file. Computer files are stored on a drive (e.g., the hard drive), disc (e.g., DVD), and a diskette (e.g., floppy disk) and may be in a folder (directory) on that medium. Its text data currently stored memory of your app. correct?
-
DaImTo over 2 yearsCan you edit your question and put up some code i want to try this for myself.
-
Richard over 2 yearsSo the file will be text on Drive. Then objects in RAM in my app. I don't have any code yet because I don't know if this even possible. All I have is the link in my question to the current google example that won't work.
-
DaImTo over 2 yearsI'm going to lean on the side that its not possible. However that being said in ten years I haven't actually tried. I will give it a try this after noon I cant flutter but if i can do it in C# you can do it in flutter.
-
-
Richard over 2 yearsThanks, but you can't use dart.io in a web app. That's the whole thing I'm trying to avoid
-
Richard over 2 yearsThanks, yes, I will try this soon. The biggest challenge I see is I can't see a way of creating a File object in Dart without passing it a path to an existing file, or creating one. Will keep looking for that part...
-
DaImTo over 2 yearsthat's the thing you don't create a file stream you create a memory stream