Skip to content

Readable and writable streams for downloading and uploading files from and to Yandex.Disk

License

Notifications You must be signed in to change notification settings

RomiC/ya-disk-stream

Repository files navigation

ya-disk-stream Tests Coverage Status

Create readable and writable streams for downloading and uploading files to Yandex.Disk.

Authorization

Each method requires an OAuth token. You can receive one manually or use one of OAuth library, i.e. passport-yandex-token.

Methods

download(token, path, [onReady], [onError])

Creates readable stream for downloading file from Yandex.Disk.

import fs from 'fs';

import { download as downloadStream } from 'ya-disk-stream';

const API_TOKEN = '9182h178d871gd8g23kwjehkwehr9';
const fileToSave = fs.createWriteStream('./Mountains.jpg');

downloadStream(API_TOKEN, 'disk:/Горы.jpg', (download) =>
  download.pipe(fileToSave)
);

upload(token, path, [overwrite=true], [onReady], [onError])

Creates writable stream for uploading file to Yandex.Disk.

import fs from 'fs';
import path from 'path';

import { upload as uploadStream } from 'ya-disk-stream';

const API_TOKEN = '9182h178d871gd8g23kwjehkwehr9';
const fileToUpload = fs.createReadStream(path.join(__dirname, 'upload.js'));

uploadStream(
  API_TOKEN,
  'disk:/upload.js',
  true,
  (stream) => fileToUpload.pipe(stream),
  (err) => process.stderr.write(err)
);

Development

Integration (feature) tests

By default feature tests aren't being run on git push. You should run the manually. In order to do that you need to create .env in the root of the project containing your API_TOKEN:

API_TOKEN=19823jd92u8h3d78efya0s7fyhaiu23ghjhg

After you may use npm test command to run all tests or npm test -- features/*.feature.js to run only feature tests.