I am trying to write a nodejs app that uploads images to an azure blob so that then another program can connect to the DB and download the image.
I am having trouble with the code I got from the blobs api. I think the upload is working properly but I dont seem to be able to download the image from the given URL.
async function execute() {
const containerName = "a00008";
const blobName = "myBlob";
const content = "./sun.png";
const localFilePath = "./sun.png";
const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const pipeline = StorageURL.newPipeline(credentials);
const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const aborter = Aborter.timeout(30 * ONE_MINUTE);
await showContainerNames(aborter, serviceURL);
await containerURL.create(aborter);
await uploadLocalFile(aborter, containerURL, content);
console.log(`Local file "${content}" is uploaded`);
await uploadStream(aborter, containerURL, localFilePath);
console.log(`Local file "${localFilePath}" is uploaded as a stream`);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, content);
console.log("The blobs URL is: " + JSON.stringify(blockBlobURL.url));
const downloadResponse = await blockBlobURL.download(aborter, 0);
downloadedContent = downloadResponse.readableStreamBody.read(content.length)
console.log(`Downloaded blob content: "${downloadedContent}"`);}
execute().then(() => console.log("Done")).catch((e) => console.log(e));
Here is the code for uploadLocalFile and uploadLocalStream they are directly taken from MS azure docs:
async function uploadLocalFile(aborter, containerURL, filePath) {
filePath = path.resolve(filePath);
const fileName = path.basename(filePath);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, fileName);
return await uploadFileToBlockBlob(aborter, filePath, blockBlobURL);
}
async function uploadStream(aborter, containerURL, filePath) {
filePath = path.resolve(filePath);
const fileName = path.basename(filePath).replace('.md', '-stream.md');
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, fileName);
const stream = fs.createReadStream(filePath, {
highWaterMark: FOUR_MEGABYTES,
});
const uploadOptions = {
bufferSize: FOUR_MEGABYTES,
maxBuffers: 5,
};
return await uploadStreamToBlockBlob(
aborter,
stream,
blockBlobURL,
uploadOptions.bufferSize,
uploadOptions.maxBuffers);
}
I tried both uploadLocalFile and uploadStream I comment 1 of them out and try the other by itself, not sure which is the correct one to use for images.
If I go to the blockBlobURL I get error: This XML file does not appear to have any style information associated with it. The document tree is shown below.
And also when I console.log the blob content I get: Downloaded blob content: "�PNG → with this weird ? before the PNG and the arrow after it.
So I am not sure what I am doing wrong, I think I am uploading correctly (not sure if I should be using uploadLocalFile or uploadLocalStream though but I think the upload is working succesfully), what am I doing wrong with the download though, the truth is I personaly wont be downloading the files just doing it to make sure it works, the other program that connects to the db will download the file. Also if you see the link for the blob after a0008 has /./ and when I place that link in the browser it removes that part, I dont know what that part /./ signifies.
Thank You