Skip to content

Instantly share code, notes, and snippets.

@Zulcom
Created May 31, 2022 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zulcom/3830cce281051910f921d8db492274ca to your computer and use it in GitHub Desktop.
Save Zulcom/3830cce281051910f921d8db492274ca to your computer and use it in GitHub Desktop.
Загрузка видео mp4 используя VK API и node js / nodejs axios vk api video upload
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