Created
May 31, 2022 20:17
-
-
Save Zulcom/3830cce281051910f921d8db492274ca to your computer and use it in GitHub Desktop.
Загрузка видео mp4 используя VK API и node js / nodejs axios vk api video upload
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
import axios from 'axios'; | |
import FormData from 'form-data'; | |
import fs from 'fs' | |
const base = 'https://api.vk.com/method/' | |
const baseParams = { | |
v: "5.131", | |
access_token: VK_TOKEN | |
} | |
const vk = axios.create({ | |
baseURL: base, | |
timeout: 1000, | |
}); | |
function upload(path, name) { | |
const method = 'video.save' | |
const params = new URLSearchParams({ | |
...baseParams, | |
name, | |
is_private: String(0), | |
wallpost: String(0), | |
privacy_view: ['nobody'], | |
privacy_comment: ['nobody'], | |
compression: String(0) | |
}) | |
const url = `${method}?${params}` | |
vk.get(url) | |
.then(({data}) => { | |
const uploadURL = data.response['upload_url'] | |
const form = new FormData(); | |
const file = fs.readFileSync(path); | |
form.append('video_file', file, { | |
filename: 'file.mp4', | |
contentType: 'video/mp4', | |
}) | |
axios.post(uploadURL, | |
form.getBuffer(), | |
{ | |
headers: form.getHeaders(), | |
maxContentLength: Infinity, | |
maxBodyLength: Infinity, | |
}) | |
.then((result) => { | |
console.log(result) | |
}) | |
.catch(console.error) | |
}) | |
.catch(console.error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment