【问题标题】:Upload a file from S3 to Youtube using node.js使用 node.js 将文件从 S3 上传到 Youtube
【发布时间】:2018-02-27 18:10:21
【问题描述】:

使用以下代码将文件从 S3 上传到 youtube:

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);
          });       });     });

不起作用,因为行:

.withMedia('video/mp4', response)

它是原始版本的替代品,有效:

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

换句话说:如果我在笔记本电脑上上传本地文件,这将起作用,因为我正在使用文件系统对象。

【问题讨论】:

  • Google API 是否允许从文件流上传?如果没有,那么您需要先将文件下载为临时文件,然后从那里上传。
  • 不知道这是否允许:(而且也没有太多关于它的文档。
  • 流到流的复制效率更高,两个 API 都支持它。否则,您可能需要将文件写入临时磁盘位置并从那里发送。
  • 您找到解决方案了吗?

标签: node.js amazon-web-services youtube amazon-s3


【解决方案1】:

如果有人正在寻找答案,这里是:

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, '  '));
    });
});

【讨论】:

    【解决方案2】:

    这个问题有点老了,但这是我如何使用 NodeJS v6.10.3 将存储在 S3 中的视频上传到 Youtube。

    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');
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 2020-04-05
      • 2016-04-22
      • 2013-09-19
      • 1970-01-01
      • 2013-08-27
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多