Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from "fs";
- import storj from "uplink-nodejs";
- import { config } from "dotenv";
- config();
- const libUplink = new storj.Uplink();
- var localFullFileName = {
- src: "./asd/nice.txt",
- dest: "./asd/image.jpg",
- };
- const {
- STORJ_API_KEY: apiKey,
- STORJ_SATELLITE_ADDRESS: satelliteURL,
- STORJ_PASSPHRASE: encryptionPassphrase,
- STORJ_BUCKET: bucketName,
- } = process.env;
- var access = await libUplink
- .requestAccessWithPassphrase(satelliteURL, apiKey, encryptionPassphrase)
- .catch((err) => {
- console.log(err);
- });
- const project = await access.openProject().catch((err) => console.log(err));
- var uploadOptions = new storj.UploadOptions();
- uploadOptions.expires = 0;
- const uploadPath = localFullFileName.src;
- const fileHandle = await project
- .uploadObject(bucketName, uploadPath, uploadOptions)
- .catch((err) => console.log(err));
- var size = {
- file: `${await fs.statSync(uploadPath).size}`,
- toWrite: 0,
- actuallyWritten: 0,
- totalWritten: 0,
- };
- const BUFFER_SIZE = Number(size.file);
- var buffer = new Buffer.alloc(BUFFER_SIZE);
- var loop = true;
- var bytesRead = 0;
- while (loop) {
- //
- size.toWrite = size.file - size.totalWritten;
- //
- if (size.toWrite > BUFFER_SIZE) {
- size.toWrite = BUFFER_SIZE;
- } else if (size.toWrite === 0) {
- break;
- }
- //
- bytesRead = await fs.readSync(
- fileHandle.upload._handle,
- buffer,
- 0,
- size.toWrite,
- size.totalWritten
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement