I created a valid “access” with read and list permissions on a bucket and then use following code to download an object from this bucket:
downloadObject: (req, res, next) => { // this is served via nodejs express
let proyecto,
descarga,
buffer = null;
access
.openProject()
.then((project) => {
proyecto = project;
let downloadOptions = new storj.DownloadOptions();
return project.downloadObject(
req.query.bucket,
req.query.object,
downloadOptions
);
})
.then((download) => {
console.log("Download Object");
console.log(download);
descarga = download;
const BUFFERSIZE = 1024 * 1024 * 1;
buffer = new Buffer.alloc(BUFFERSIZE);
return download.info();
})
.then((info) => {
console.info("Download object info:");
console.info(info);
return descarga.read(buffer, buffer.length);
})
.then((bytesread) => {
console.log("byteread object");
console.log(bytesread);
return descarga.close();
})
.then(() => {
console.log("Download closed");
return proyecto.close();
})
.then(() => {
console.log("Project closed");
res.writeHead(200, {
"Content-Type": "image/jpeg",
"Content-disposition": "attachment;filename=" + req.query.object,
"Content-Length": buffer.length,
});
res.end(Buffer.from(buffer, "binary"));
})
.catch((err) => {
console.error("Error:");
console.error(err);
if (descarga != null) {
descarga
.close()
.then(() =>
proyecto
.close()
.then(() => console.debug("Download and project closed."))
);
} else {
if (proyecto != null) {
proyecto.close().then(() => console.debug("Project closed."));
}
}
res.status(500).json(err);
});
}
The result that I get is:
Download object info:
{
custom: { entries: { ‘0’: [Object], ‘1’: [Object] }, count: 2 },
system: { created: 1672786094, expires: 0, content_length: 321283 },
is_prefix: 0,
key: ‘ViewOfMurnau_Kandinsky.jpg’
}
Error:
InternalError: internal error
at Object.storjException (/home/juan/Yaluba/Backend/node_modules/uplink-nodejs/dist/error.js:121:21)
at /home/juan/Yaluba/Backend/node_modules/uplink-nodejs/dist/download.js:19:25
at async DownloadResultStruct.read (/home/juan/Yaluba/Backend/node_modules/uplink-nodejs/dist/download.js:18:27) {
code: 2,
details: ‘’
}
Download and project closed.
I have tried a few things but I can’t find why it crashed at the point when I call the “read” method on the download object. I can’t get any clue from the uplink-nodejs module’s source code, but since I am new to storj dev, I am sure I am missing some point.