Advertisement
Guest User

Untitled

a guest
Jan 19th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from "fs";
  2. import storj from "uplink-nodejs";
  3. import { config } from "dotenv";
  4. config();
  5.  
  6. const libUplink = new storj.Uplink();
  7. var localFullFileName = {
  8.   src: "./asd/nice.txt",
  9.   dest: "./asd/image.jpg",
  10. };
  11.  
  12. const {
  13.   STORJ_API_KEY: apiKey,
  14.   STORJ_SATELLITE_ADDRESS: satelliteURL,
  15.   STORJ_PASSPHRASE: encryptionPassphrase,
  16.   STORJ_BUCKET: bucketName,
  17. } = process.env;
  18.  
  19. var access = await libUplink
  20.   .requestAccessWithPassphrase(satelliteURL, apiKey, encryptionPassphrase)
  21.   .catch((err) => {
  22.     console.log(err);
  23.   });
  24.  
  25. const project = await access.openProject().catch((err) => console.log(err));
  26.  
  27. var uploadOptions = new storj.UploadOptions();
  28. uploadOptions.expires = 0;
  29. const uploadPath = localFullFileName.src;
  30.  
  31. const fileHandle = await project
  32.   .uploadObject(bucketName, uploadPath, uploadOptions)
  33.   .catch((err) => console.log(err));
  34.  
  35. var size = {
  36.   file: `${await fs.statSync(uploadPath).size}`,
  37.   toWrite: 0,
  38.   actuallyWritten: 0,
  39.   totalWritten: 0,
  40. };
  41. const BUFFER_SIZE = Number(size.file);
  42. var buffer = new Buffer.alloc(BUFFER_SIZE);
  43. var loop = true;
  44. var bytesRead = 0;
  45.  
  46. while (loop) {
  47.   //
  48.   size.toWrite = size.file - size.totalWritten;
  49.   //
  50.   if (size.toWrite > BUFFER_SIZE) {
  51.     size.toWrite = BUFFER_SIZE;
  52.   } else if (size.toWrite === 0) {
  53.     break;
  54.   }
  55.   //
  56.   bytesRead = await fs.readSync(
  57.     fileHandle.upload._handle,
  58.     buffer,
  59.     0,
  60.     size.toWrite,
  61.     size.totalWritten
  62.   );
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement