Created
September 23, 2016 08:43
-
-
Save donpark/fe5e3aa3aa4f19171d1bc98fafed774b to your computer and use it in GitHub Desktop.
node-slack-sdk example showing how to upload a S3 stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var url = require('url'); | |
var request = require('request'); | |
var WebClient = require('@slack/client').WebClient; | |
var token = process.env.SLACK_API_TOKEN || ''; | |
var web = new WebClient(token); | |
var s3Url = 'https://testbucket.s3-us-west-2.amazonaws.com/some/file/in/S3.mp4'; | |
var s3Stream = // from s3.GetObject call | |
var fileName = path.basename(url.parse(fileUrl).pathname); | |
// Slack WebClient uses 'request' module which in turn uses 'form-data' module | |
// which does seeminly unnecessary check for 'path' property of the stream. | |
// Related issue: https://github.com/request/request/pull/1964 | |
// Related code: https://github.com/form-data/form-data/blob/master/lib/form_data.js#L98 | |
// HACK: workaround the problem by setting a bogus path value | |
s3Stream.path = 'bogus/path/to/workaround/form-data/path/check' | |
// File upload via file param | |
var streamOpts = { | |
file: s3Stream | |
}; | |
web.files.upload(fileName, streamOpts, function handleStreamFileUpload(err, res) { | |
if (err) { | |
console.error(err); | |
} else { | |
console.log(res); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment