【问题标题】:Is there a way to upload file to google drive from remote url - C#有没有办法从远程 url 上传文件到谷歌驱动器 - C#
【发布时间】:2020-09-03 15:57:44
【问题描述】:

基本上,我在远程服务器上有一些视频,我想上传到谷歌驱动器而不将它们下载到客户端。只需直接将它们上传到谷歌驱动程序。我在 stackoverflow 上阅读了google drive api .net documentation 和一些问题,但他们没有提到任何此类内容。至少在我读过的问题中。我在 .net 框架 Windows 窗体应用程序上执行此操作。

【问题讨论】:

  • 您好,您可以展示您的代码示例吗?
  • 我还没有开始在google驱动代码。我先看看我想要的是否可行,因为如果不是,我可能不会使用谷歌驱动器。

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


【解决方案1】:

要使上传工作,您需要在运行代码的机器上拥有该文件。您必须先将它们下载给自己,然后再将它们上传到谷歌驱动器。

{
    // Create the service using the client credentials.
    var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "APP_NAME_HERE"
        });
    var uploadStream = new System.IO.FileStream("FILE_NAME",
                                                System.IO.FileMode.Open,
                                                System.IO.FileAccess.Read);
    // Get the media upload request object.
    InsertMediaUpload insertRequest = service.Files.Insert(
        new File
        {
            Title = "FILE_TITLE",
        },
        uploadStream,
        "image/jpeg");

    // Add handlers which will be notified on progress changes and upload completion.
    // Notification of progress changed will be invoked when the upload was started,
    // on each upload chunk, and on success or failure.
    insertRequest.ProgressChanged += Upload_ProgressChanged;
    insertRequest.ResponseReceived += Upload_ResponseReceived;

    var task = insert.UploadAsync();
    task.ContinueWith(t =>
        {
            // Remeber to clean the stream.
            uploadStream.Dispose();
        });
}

static void Upload_ProgressChanged(IUploadProgress progress)
{
    Console.WriteLine(progress.Status + " " + progress.BytesSent);
}

static void Upload_ResponseReceived(File file)
{
    Console.WriteLine(file.Title + " was uploaded successfully");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多