【问题标题】:Problem downloading file from Google Drive using service account使用服务帐户从 Google Drive 下载文件时出现问题
【发布时间】:2022-09-14 17:59:31
【问题描述】:

这段代码让我摸不着头脑好几天了。

我正在尝试使用 Google Drive 和服务帐户为我的应用程序实现自动更新。

在我的 Google Drive 中有一个名为 Update 的文件夹,它与服务帐户共享。 在 Update 文件夹中有两个文件,一个名为 Version.txt 的文本文件,它是最新版本号的字符串,以及一个可执行文件 Update.exe,它是最新的应用程序版本。

我可以很好地读取 Version.txt 文件,但是当我尝试下载 ~1MB 可执行文件时,似乎在下载文件的过程中会出现延迟,但之后内存流 ms2 始终为空。

有人有想法么?这可能与服务帐户身份验证有关,但我不确定如何解决。

public class GoogleDriveAccess
{
    //google drive file IDs
    private readonly static string versionFileID = @"blahblahblahblah";
    private readonly static string updateFileID = @"blehblehblehbleh";

    //check for updated assembly on google drive and install if authorised
    public async static void CheckForUpdate()
    {
        try
        {
            //load json key file from resources into a byte array
            var jsonFile = Properties.Resources.drive_access;

            //create service account keyfile from json byte array
            var serviceAccountKeyfile = System.Text.Encoding.Default.GetString(jsonFile);

            //create Google credentials with full access from service account keyfile
            GoogleCredential credential = GoogleCredential.FromJson(serviceAccountKeyfile)
                .CreateScoped(new[] { DriveService.ScopeConstants.Drive });

            //create Google drive service using credentials
            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

            //get version file metadata
            var getRequest = service.Files.Get(versionFileID);

            //download version file to memory
            await using var ms = new MemoryStream();
            await getRequest.DownloadAsync(ms);

            //read memory stream into a string
            ms.Position = 0;
            var sr = new StreamReader(ms);
            string textUpdateVersion = sr.ReadToEnd().Trim();

            //obtain our current assembly version
            Version currentVersion = Assembly.GetEntryAssembly().GetName().Version;

            //create an assembly version from the downloaded version text file
            Version updateVersion = new Version(textUpdateVersion);

            //if downloaded version is higher than our current version then request to download new assembly
            if (updateVersion > currentVersion)
            {
                //prompt user to commence update
                DialogResult result = MessageBox.Show($"Would you like to update to the latest version {updateVersion}?", Properties.Resources.AppName, MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    //get update file metadata
                    getRequest = service.Files.Get(updateFileID);

                    //download update file to memory
                    await using var ms2 = new MemoryStream();
                    await getRequest.DownloadAsync(ms2);

                    //***ms2 is always empty here***

                    //convert file to binary
                    ms2.Position = 0;
                    var br = new BinaryReader(ms2);
                    byte[] bin = br.ReadBytes(Convert.ToInt32(ms2.Length));

                    //rest of code

                }
            }
        }
        catch
        {
           //deal with any exceptions
        }
    }
}

【问题讨论】:

  • 我会尝试其他一些文件类型。我认为您可能陷入了 .exe 文件不被认为是安全的,并且存在无法下载的陷阱。您是否尝试过从网络应用程序下载它?
  • 这只是更改文件扩展名的问题吗?我最初上传的文件没有扩展名,但这也不起作用。
  • 这是官方下载示例。 media_download 看看有没有帮助。
  • 哦,我尝试从 Web 应用程序下载,它认为它感染了病毒,所以我需要使用我期望的 acknowledgeAbuse 标志。

标签: c# winforms google-drive-api .net-6.0 google-api-dotnet-client


【解决方案1】:

file.get 有一个可选的婴儿车

尝试将其设置为 true

getRequest = service.Files.Get(updateFileID);
getRequest.AcknowledgeAbuse  = true;

然后看看它是否不会让你下载它。

【讨论】:

  • 我下班回家后会试试看,如果可行,我会接受你的回答。
  • 如果它不让我知道,我会测试你的代码。
  • 它没有用,但请参阅我自己的答案。还要感谢媒体下载示例。我没有看到那个,但它让ProgressChanged 事件对我来说更加清晰。
【解决方案2】:

最后,谷歌似乎认为我的文件感染了病毒。

添加getRequest.AcknowledgeAbuse 并没有改变任何东西,结果是一样的。

将文件扩展名从 .exe 更改为 .bin 也没有做任何事情,因为 Web 应用程序仍将文件名视为 update.bin.exe

上传的文件是一个可执行的.zip 文件。似乎 GD 不喜欢这些类型的文件。

上传一个重命名为.bin 的非压缩.exe 文件似乎已经解决了这个问题。

现在我的流终于有了文件内容。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2021-07-04
    • 2015-03-21
    相关资源
    最近更新 更多