【发布时间】:2022-08-28 23:36:12
【问题描述】:
我目前正在开发一个 C# 软件,它会压缩一些文件,然后将它们上传到我的谷歌驱动器。
我遵循了 Google Drive API 的本教程:How to upload a file to Google Drive with C# .net
问题是上传失败,因为 HTTP 客户端超时。
有人知道吗?
是驱动器上文件创建中的“文本/纯文本”吗?
private static string pathToServiceAccountKeyFile = @"C:\Users\X\Downloads\File.json";
private const string serviceAccountEmail = XXX";
private static string uploadFileName = @$"{zipFilesStorageDir}\{date}.zip";
private const string directoryID = "XXX";
private static void FileUpload()
{
uploadFileName = @$"{zipFilesStorageDir}\{date}.zip";
// Load the Service account credentials and define the scope of its access.
var credential = GoogleCredential.FromFile(pathToServiceAccountKeyFile)
.CreateScoped(DriveService.ScopeConstants.Drive);
// Create the Drive service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
// Upload file Metadata
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = date + ".zip",
Parents = new List<string> { directoryID }
};
string uploadedFileId;
// Create a new file on Google Drive
using (var fsSource = new FileStream(uploadFileName, 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 = request.Upload();
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;
}
【问题讨论】:
标签: c# google-api google-drive-api google-api-dotnet-client