简体   繁体   中英

Unpack header from AWS call in Node.js

I have a fully working project in PHP. I am trying to make the equivalent to this project in Node.js. The project relies on pulling data from AWS and then unpacking a header from that data. In PHP this is done with the following:

$request = @self::$s3->getObject(self::$bucket, self::$filepath,false,0, self::HeaderSize+ 5 * self::ResolutionEntrySize + self::TileCountSize + 256 * self::TileEntrySize);

// Open the PAR file.
if( is_object($request) && ($request->code < 400)) {
    self::$modifiedTime = $request->headers['time'];

    // Read and unpack the header.
    self::$header = unpack('I*',  substr($request->body, 0, self::HeaderSize));
    self::$resolutionEntry = substr($request->body, self::HeaderSize, self::ResolutionEntrySize);
    self::$resolutionTable = substr($request->body, self::HeaderSize + 5 * self::ResolutionEntrySize, self::TileCountSize + 256 * self::TileEntrySize);
}

I am unsure of how to perform the same operation in Node.js. I have tried the following:

aws.getObject(params, function(err, data){
    if(err == null){
        var objectData = data.Body.toString('utf-8'); 
        var sub = objectData.substring(0, 28); 
        var header = bufferpack.unpack('I*', sub);
       }
   }); 

In PHP, the header comes back as an array of numbers, however in Node it is saying it is null . I know that the AWS call is working since data is returned as the following:

{ 
  AcceptRanges: 'bytes',
  LastModified: 'Wed, 06 Apr 2016 20:04:02 GMT',
  ContentLength: '1602862',
  ETag: '9826l1e5725fbd52l88ge3f5v0c123a4"',
  ContentType: 'application/octet-stream',
  Metadata: {},
  Body: <Buffer 01 00 00 00  ... > }

Am I using Bufferpack incorrectly?

Yes, Bufferpack is being used incorrectly. Bufferpack accepts a buffer, not a string, so instead of passing in sub , you should pass in data.Body . Also, the * notation is not recognized, so instead use the number of bytes you want, such as

var header = bufferpack.unpack('7I', data.Body);

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