Categories
JavaScript Answers

How to get response from S3 getObject in Node.js?

Sometimes, we want to get response from S3 getObject in Node.js.

In this article, we’ll look at how to get response from S3 getObject in Node.js.

How to get response from S3 getObject in Node.js?

To get response from S3 getObject in Node.js, we can get the file chunks with the GetObjectCommand constructor.

For instance, we write

const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const client = new S3Client();

const getObject = (Bucket, Key) => {
  return new Promise(async (resolve, reject) => {
    const getObjectCommand = new GetObjectCommand({ Bucket, Key });
    try {
      const response = await client.send(getObjectCommand);
      let responseDataChunks = [];
      response.Body.once("error", (err) => reject(err));
      response.Body.on("data", (chunk) => responseDataChunks.push(chunk));
      response.Body.once("end", () => resolve(responseDataChunks.join("")));
    } catch (err) {
      return reject(err);
    }
  });
};

to create a GetObjectCommand object with an object with the Bucket and Key of the file to get.

Then we call client.send with getObjectCommand to send the command to get the file.

Then we call response.Body.on with 'data' to get the data chunks with the callback.

And then we call response.Body.once with 'end' to join the chunks together and call resolve with the joined chunks to use that as the value of the returned promise.

Conclusion

To get response from S3 getObject in Node.js, we can get the file chunks with the GetObjectCommand constructor.

Categories
JavaScript Answers

How to convert Base64 string to JavaScript file object like as from file input form?

Sometimes, we want to convert Base64 string to JavaScript file object like as from file input form.

In this article, we’ll look at how to convert Base64 string to JavaScript file object like as from file input form.

How to convert Base64 string to JavaScript file object like as from file input form?

To convert Base64 string to JavaScript file object like as from file input form, we can use fetch.

For instance, we write

const urlToFile = async (url, filename, mimeType) => {
  const res = await fetch(url);
  const buf = await res.arrayBuffer();
  return new File([buf], filename, { type: mimeType });
};

(async () => {
  const file = await urltoFile(
    "data:text/plain;base64,aGVsbG8gd29ybGQ=",
    "hello.txt",
    "text/plain"
  );
  console.log(file);
})();

to define the urlToFile function that calls fetch with the url to return a promise with the res response object.

Then we call res.arrayBuffer to get the response as an array buffer.

Next, we create a File object with the buf buffer with the given mimeType and return that as the resolve value of the promise returned by urlToFile.

Then we call urlToFile with the base64 URL, file name, and the MIME type of the file to return a promise with the file.

Conclusion

To convert Base64 string to JavaScript file object like as from file input form, we can use fetch.

Categories
JavaScript Answers

How to get the last item in a JavaScript object?

Sometimes, we want to get the last item in a JavaScript object.

In this article, we’ll look at how to get the last item in a JavaScript object.

How to get the last item in a JavaScript object?

To get the last item in a JavaScript object, we can use the Object.values method.

For instance, we write

const fruitValues = Object.values(fruitObject);
const last = fruitValues[fruitValues.length - 1];

to call Object.values with the fruitObject object to return an array of property values from fruitObject.

Then we get the last value from the array with fruitValues[fruitValues.length - 1] to get the last item in the fruitObject object.

Conclusion

To get the last item in a JavaScript object, we can use the Object.values method.

Categories
JavaScript Answers

How to do the equivalent of array push() method with objects with JavaScript?

Sometimes, we want to do the equivalent of array push() method with objects with JavaScript.

In this article, we’ll look at how to do the equivalent of array push() method with objects with JavaScript.

How to do the equivalent of array push() method with objects with JavaScript?

To do the equivalent of array push() method with objects with JavaScript, we can add properties to objects with assignment.

For instance, we write

const tempData = {};
for (const [index, d] of data.entries()) {
  if (d.status === "Valid") {
    tempData[index] = d;
  }
}

to create the tempData object

Then we call data.entries to return an array with key-value pair entries from the data array.

And then we use a for-of loop to check the d.status property value.

And if it’s 'Valid', then we add the index property to tempData and assign d to it.

Conclusion

To do the equivalent of array push() method with objects with JavaScript, we can add properties to objects with assignment.

Categories
JavaScript Answers

How to get text element width of an SVG with JavaScript?

Sometimes, we want to get text element width of an SVG with JavaScript.

In this article, we’ll look at how to get text element width of an SVG with JavaScript.

How to get text element width of an SVG with JavaScript?

To get text element width of an SVG with JavaScript, we can use the getBBox method.

For instance, we write

const bbox = textElement.getBBox();
const width = bbox.width;
const height = bbox.height;

to call textElement.getBBox to get the bbox object from the text element in the SVG.

Then we get the dimensions from the width and height properties of bbox.

Conclusion

To get text element width of an SVG with JavaScript, we can use the getBBox method.