Skip to content

Instantly share code, notes, and snippets.

@mjpitz
Last active February 15, 2022 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjpitz/6237acf1137c1900c21e9a3fb897ba99 to your computer and use it in GitHub Desktop.
Save mjpitz/6237acf1137c1900c21e9a3fb897ba99 to your computer and use it in GitHub Desktop.
Pseudocode for performing an upload using a chunked HTTP request
// The guide online generally works, but has values that do not correspond to the primary active satellites.
// https://github.com/storj-thirdparty/uplink-nodejs/blob/7fb4a9ab35b04c7338d503d6d3d76d9264c53867/docs/tutorial.md
const http = require('http');
// Step 2 / 3
const storj = require("uplink-nodejs");
const uplink = new storj.Uplink();
// Step 1
const config = {
satelliteURL : "us1.storj.io",
apiKey : "api-key",
passphrase : "encryption/decreption passphrase",
bucketName : "myapp",
}
async function main() {
// Step 4
const access = await uplink.requestAccessWithPassphrase(config.satelliteURL, config.apiKey, config.passphrase)
// Step 5
const project = await access.openProject();
const handle = async (req, resp) => {
// Step 8
const uploadOptions = new storj.UploadOptions();
uploadOptions.expires = 0;
const upload = await project.uploadObject(config.bucketName, "file/path", uploadOptions);
req.on("data", (chunk) => {
upload.write(chunk, chunk.length).catch((err) => {
resp.writeHead(500);
resp.write(err);
resp.end();
});
});
req.on("close", () => {
upload.commit()
.then(() => {
resp.writeHead(200);
resp.end();
})
.catch((err) => {
resp.writeHead(500);
resp.write(err);
resp.end();
});
});
}
// start an http server
http.createServer((req, resp) => {
handle(req, resp).catch((err) => {
resp.writeHead(500);
resp.write(err);
resp.end();
});
}).listen(3456);
}
main().catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment