我無法解決如何在C#YouTube API的V3中恢復被中斷的上傳。YouTube C#API V3,您如何恢復中斷的上傳?
我現有的代碼使用V1和正常工作,但我切換到V3。
如果我在不更改任何內容的情況下調用UploadAsync(),它將從頭開始。使用Fiddler,我可以看到沒有遵循protocol given here,上傳重新開始。
我試着根據V1設置流內的位置,但沒有ResumeAsync()方法可用。
Python示例使用NextChunk,但SendNextChunk方法受保護,並且在C#中不可用。
在下面的代碼中,UploadVideo()和Resume()工作正常,如果我讓他們完成,但整個視頻上傳,而不是其他部分。
如何使用google.apis.youtube.v3恢復中斷上傳?
這是我到目前爲止嘗試過的C#代碼。
private ResumableUpload<Video> UploadVideo(
YouTubeService youTubeService, Video video, Stream stream, UserCredential userCredentials)
{
var resumableUpload = youTubeService.Videos.Insert(video,
"snippet,status,contentDetails", stream, "video/*");
resumableUpload.OauthToken = userCredentials.Token.AccessToken;
resumableUpload.ChunkSize = 256 * 1024;
resumableUpload.ProgressChanged += resumableUpload_ProgressChanged;
resumableUpload.ResponseReceived += resumableUpload_ResponseReceived;
resumableUpload.UploadAsync();
return resumableUpload;
}
private void Resume(ResumableUpload<Video> resumableUpload)
{
//I tried seeking like V1 but it doesn't work
//if (resumableUpload.ContentStream.CanSeek)
// resumableUpload.ContentStream.Seek(resumableUpload.ContentStream.Position, SeekOrigin.Begin);
resumableUpload.UploadAsync(); // <----This restarts the upload
}
void resumableUpload_ResponseReceived(Video obj)
{
Debug.WriteLine("Video status: {0}", obj.Status.UploadStatus);
}
void resumableUpload_ProgressChanged(IUploadProgress obj)
{
Debug.WriteLine("Position: {0}", (resumableUploadTest == null) ? 0 : resumableUploadTest.ContentStream.Position);
Debug.WriteLine("Status: {0}", obj.Status);
Debug.WriteLine("Bytes sent: {0}", obj.BytesSent);
}
private void button2_Click(object sender, EventArgs e)
{
Resume(resumableUploadTest);
}
任何解決方案/建議/演示或指向「google.apis.youtube.v3」源代碼的鏈接將非常有幫助。
在此先感謝!
編輯:新的信息
我仍然在做這個,我相信API根本沒有完成。無論是我還是缺少一些簡單的東西。
我仍然無法找到「google.apis.youtube.v3」源代碼,因此我下載了最新的「google-api-dotnet-client」源代碼。這包含YouTube API使用的ResumableUpload類。
我設法通過跳過UploadAsync()方法中的前四行代碼成功地繼續上傳。我創建了一個名爲ResumeAsync()的新方法,它的前四行初始化代碼被刪除,它是UploadAsync()的副本。一切正常,上傳從原來的位置恢復並完成。
我寧願不改變API中的代碼,所以如果有人知道我應該如何使用它,請告訴我。
我會繼續堵塞,看看我能否解決問題。
這是原始的UploadAsync()方法和我的ResumeAsync()hack。
public async Task<IUploadProgress> UploadAsync(CancellationToken cancellationToken)
{
try
{
BytesServerReceived = 0;
UpdateProgress(new ResumableUploadProgress(UploadStatus.Starting, 0));
// Check if the stream length is known.
StreamLength = ContentStream.CanSeek ? ContentStream.Length : UnknownSize;
UploadUri = await InitializeUpload(cancellationToken).ConfigureAwait(false);
Logger.Debug("MediaUpload[{0}] - Start uploading...", UploadUri);
using (var callback = new ServerErrorCallback(this))
{
while (!await SendNextChunkAsync(ContentStream, cancellationToken).ConfigureAwait(false))
{
UpdateProgress(new ResumableUploadProgress(UploadStatus.Uploading, BytesServerReceived));
}
UpdateProgress(new ResumableUploadProgress(UploadStatus.Completed, BytesServerReceived));
}
}
catch (TaskCanceledException ex)
{
Logger.Error(ex, "MediaUpload[{0}] - Task was canceled", UploadUri);
UpdateProgress(new ResumableUploadProgress(ex, BytesServerReceived));
throw ex;
}
catch (Exception ex)
{
Logger.Error(ex, "MediaUpload[{0}] - Exception occurred while uploading media", UploadUri);
UpdateProgress(new ResumableUploadProgress(ex, BytesServerReceived));
}
return Progress;
}
public async Task<IUploadProgress> ResumeAsync(CancellationToken cancellationToken)
{
try
{
using (var callback = new ServerErrorCallback(this))
{
while (!await SendNextChunkAsync(ContentStream, cancellationToken).ConfigureAwait(false))
{
UpdateProgress(new ResumableUploadProgress(UploadStatus.Uploading, BytesServerReceived));
}
UpdateProgress(new ResumableUploadProgress(UploadStatus.Completed, BytesServerReceived));
}
}
catch (TaskCanceledException ex)
{
UpdateProgress(new ResumableUploadProgress(ex, BytesServerReceived));
throw ex;
}
catch (Exception ex)
{
UpdateProgress(new ResumableUploadProgress(ex, BytesServerReceived));
}
return Progress;
}
這些是顯示上傳恢復的Fiddler records。
現在,Google API v3 .NET客戶端庫包含一個功能,用於在ResumableUpload期間保存UploadUri,並在稍後使用UploadUri在程序重新啓動時恢復上載。 https://github.com/google/google-api-dotnet-client-samples中有兩個ResumableUpload示例 –