[英]Is there a way to upload file to google drive from remote url - C#
基本上,我在遠程服務器上有一些視頻,我想上傳到谷歌驅動器而不將它們下載到客戶端。 只需直接將它們上傳到谷歌驅動程序。 我閱讀了谷歌驅動器 api .net 文檔和一些關於 stackoverflow 的問題,但他們沒有提到任何此類內容。 至少在我讀過的問題中。 我在 .net 框架 windows forms 應用程序上執行此操作。
要使上傳工作,您需要在運行代碼的機器上擁有該文件。 您必須先將它們下載給自己,然后再將它們上傳到谷歌驅動器。
{
// 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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.