简体   繁体   中英

What's Unit8Array in AWS S3 bucket response

I am calling the AWS S3 getObject function: (AWS.Request) getObject(params = {}, callback)

I wrote this:

getMenuJson() {
   this.s3.getObject({
     Bucket: 'improd-image-pipeline',
     Key: 'remoteUX/qa/menus/v1/menus.json',
     ResponseContentType: 'application/json',
   }, (err, data) => {
     if (err) {
       this.$log.log(err);
     } else {
       this.$log.log(data, JSON.parse(data.Body));
     }
   });
 }

This is the console output, I try to print the data and the actual menus.json file on the S3 console. 在此输入图像描述

The return data if I printed like this: JSON.parse(data.Body), this is the result.

 {  
   "menus":[  
      {  
         "name": "Flat Collections Example",
         "menuId": "aaa-3656-4a32-bdda-e2e016cf35ee"
      },
      {  
         "name":"P3",
         "menuId": "aaa-f5fc-4e18-9089-47e43a4237e8"
      },
      {  
         "name": "Tabbed Menu Example",
         "menuId": "aaa-768a-40bd-a9f4-b633f3679c36"
      }
   ]
}

But, how to understand the response body: Unit8Array, and how it's interpreted to a actual json formatted data?

I am using Angular.toJson(data.Body) and this function didn't understand it, but JSON.parse(data.Body) will get the correct result.

It's a JavaScript Typed Array which represents an array of 8-bit unsigned integers.

AWS.Request.getObject() return value of data.Body can be any of the following: Buffer, Typed Array, Blob, String, ReadableStream.

You can check AWS docs for more info.

In your case it's Uint8Array (JavaScript typed array).

Understand more: JavaScript Typed Arrays , Uint8Array object

I would recommend to use AWS SDK for javascript. http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/working-with-json.html

The AWS SDK for JavaScript provides a JavaScript API for AWS services. Using the SDK, you can build JavaScript applications for the browser. The JavaScript API allows developers to build libraries or applications that make use of AWS services.

The script to use is here: https://sdk.amazonaws.com/js/aws-sdk-2.6.10.min.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM