Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunked media upload example #185

Merged

Conversation

lyzadanger
Copy link

Hello! This PR adds a multi-step chunked media upload example.

@desmondmorris
Copy link
Owner

@lyzadanger this is great! Thanks for the contribution!

@desmondmorris desmondmorris merged commit 808a8b0 into desmondmorris:master Aug 23, 2016
@ashatat
Copy link

ashatat commented Feb 3, 2019

@lyzadanger @desmondmorris
I am stuck on this
I see in this example you send the file in one chunk, what if there was more than one?
I tried it and I got HTTP Error: 400 Bad Request

here is my code:

const streamFile = fs.createReadStream(file.path, { highWaterMark: 1024 * 1024 });
      function makePost(endpoint, params) {
        return new Promise((resolve, reject) => {
          client.post(endpoint, params, (error, data, response) => {
            if (error) {
              console.log(error)
              reject(error);
            } else {
              resolve(data);
            }
          });
        });
      }
      makePost('media/upload', {
        command: 'INIT',
        total_bytes: file.size,
        media_type: file.mimetype,
      })
        .then((data) => {
          console.log('INIT', data);
          let counter = 0;
          streamFile.on('data', async (chunk) => {
            console.log('counter', counter);
            console.log('chunk', data.media_id_string);
            makePost('media/upload', {
              command: 'APPEND',
              media_id: data.media_id_string,
              media: chunk,
              segment_index: counter,
            }).then(appendData => console.log('APPEND', appendData));
            counter += 1;
          });
          streamFile.on('end', () => {
            makePost('media/upload', {
              command: 'FINALIZE',
              media_id: data.media_id_string,
            })
              .then(result => console.log(result)).catch(error => console.log(error));
          });
        });

what I am missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants