Take the 2-minute tour ×

Whenever I try to upload a large video via Direct Upload using the YouTube API. I get an OutOfMemory Exception. Is there anything I can do to get rid of this? The YouTube API does not say anything about video size limit using direct upload.

I gave up on the Direct Upload. Now I trying the resumable upload way. My code is below.

YouTubeRequest request;
YouTubeRequestSettings settings = new YouTubeRequestSettings("YouTube Upload", Client Key, "Username", "Password");
request = new YouTubeRequest(settings);
Video newVideo = new Video();

ResumableUploader m_ResumableUploader = null;
Authenticator YouTubeAuthenticator;

m_ResumableUploader = new ResumableUploader(256); //chunksize 256 kilobyte
m_ResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(m_ResumableUploader_AsyncOperationCompleted);
m_ResumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(m_ResumableUploader_AsyncOperationProgress);

YouTubeAuthenticator = new ClientLoginAuthenticator("YouTubeUploader", ServiceNames.YouTube, "kjohnson@resoluteinnovations.com", "password");

//AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/uploads");
//link.Rel = ResumableUploader.CreateMediaRelation;

System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] chunk = new byte[256000];int count = 1;
while (true) {
    int index = 0;
    while (index < chunk.Length) {
        int bytesRead = stream.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0) {
        index += bytesRead;
    if (index != 0) { // Our previous chunk may have been the last one
        newVideo.MediaSource = new MediaFileSource(new MemoryStream(chunk), filePath, "video/quicktime");
        if (count == 1) {
            m_ResumableUploader.InsertAsync(YouTubeAuthenticator, newVideo.YouTubeEntry, new MemoryStream(chunk));
            m_ResumableUploader.ResumeAsync(YouTubeAuthenticator, new Uri("http://uploads.gdata.youtube.com/resumable/feeds/api/users/uploads"), "POST", new MemoryStream(chunk), "video/quicktime", new object());
    if (index != chunk.Length) { // We didn't read a full chunk: we're done

Can anyone tell me what is wrong? My 2 GB video not uploading.

share|improve this question
What is your code? What did you tried? What is your error code? –  Soner Gönül Dec 13 '12 at 14:23
Where is the exception throw? Perhaps just provide the stacktrace –  Christopherous 5000 Dec 17 '12 at 15:11
Line: m_ResumableUploader.InsertAsync(...) Error: System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.HttpWebRequest.GetResponse() –  Kyle Johnson Dec 17 '12 at 18:55
add comment

2 Answers

up vote 5 down vote accepted

The reason I was getting a 403 Forbidden error was due to the fact that I was not passing in:

  1. Username & Password
  2. A developer key

The request variable in the code above is not being used/sent in the upload. Therefore I was doing an unauthorized upload.

share|improve this answer
add comment

Chances are that you are not disposing your objects. Ensure all disposable objects are within a using statement..

For example, this code will upload a large zip file to a server:

    using (Stream ftpStream = FTPRequest.GetRequestStream())
        using (FileStream file = File.OpenRead(ImagesZipFile))
            // set up variables we'll use to read the file
            int length = 1024;
            byte[] buffer = new byte[length];
            int bytesRead = 0;

            // write the file to the request stream
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer, 0, bytesRead);
            while (bytesRead != 0);
catch (Exception e)
    // throw the exception
    throw e;
share|improve this answer
add comment

Your Answer


