Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file from S3 to Youtube using node.js

Uploading a file from S3 to youtube, using this code:

s3.setBucket('MyBucket'); 
s3.get('MyBucket/5/' + filename, null, 'stream', function(err, response) {  
    googleapis.discover('youtube', 'v3').execute(function(err, client) {
      var metadata = {
              snippet: { title: title, description: description}, 
                status: { privacyStatus: 'public' }
      };
      client
          .youtube.videos.insert({ part: 'snippet,status'}, metadata)
          .withMedia('video/mp4', response)
          .withAuthClient(auth)
          .execute(function(err, result) {
            if (err) console.log(err);
            else console.log(JSON.stringify(result, null, '  '));
            response.redirect('/share?set=yt&id=' + result.id);
          });       });     });

does not work, because the line:

.withMedia('video/mp4', response)

It's a replacement of the original one, that works:

fs.readFileSync('/temp/myfile.png')

In other words: If I upload a local file on my laptop, this will work because I'm using the filesystem object.

like image 895
user3465009 Avatar asked Mar 26 '14 15:03

user3465009


2 Answers

In case anyone is looking for the answer, here it is:

var googleapis = require('googleapis'),
    OAuth2 = googleapis.auth.OAuth2,
    ytdapi = googleapis.youtube('v3'),
    AWS = require('aws-sdk'),
    s3  = new AWS.S3;

var s3data = {
    Bucket: 'BUCKET_NAME',
    Key: 'VIDEO_NAME'
};

s3.getObject(s3data, function (err, data) {
    var params = {
        auth: oauth2Client,
        part: 'snippet,status',
        resource: {
            snippet: {
                title: 'Title',
                description: 'Description'
            },
            status: {
                privacyStatus: 'public'
            }
        },
        media: {
            mimeType: 'video/mp4',
            body: data.Body
        }
    };
    
    ytdapi.videos.insert(params, function (err, result) {
        if (err) console.log(err);
        else console.log(JSON.stringify(result, null, '  '));
    });
});
like image 163
AboulEinein Avatar answered Sep 21 '22 13:09

AboulEinein


This question is a bit old but here is how I uploaded a video stored in S3 to Youtube using NodeJS v6.10.3.

const AWS = require('aws-sdk'); // v1.6.0
const s3 = new AWS.S3({region: 'eu-west-1', apiVersion: '2006-03-01'});
const {google} = require('googleapis'); // v26.0.1
const YoutubeApi = google.youtube('v3');

const s3data = {
    Bucket: bucketName,
    Key: fileName
};
let fileStream = s3.getObject(s3data).createReadStream();
const params = {
    auth: oauth2Client, // create this using 'google-auth-library'
    part: 'snippet,status',
    resource: {
        snippet: {
            title: 'Title',
            description: 'description'
        },
        status: {
            privacyStatus: 'private'
        }
    },
    media: {
        mimeType: 'video/mp4',
        body: fileStream // stream to stream copy
    }
};
YoutubeApi.videos.insert(params, function (err, result) {
    if (err) {
        console.error('error');
        console.error(err);
    }
    else {
        console.log('success');
    }
});
like image 29
seza443 Avatar answered Sep 20 '22 13:09

seza443