Nodejs: create signature request header for AWS API


Step 1: Install package sign-aws-requests

npm i sign-aws-requests

Step 2: example code

const sign = createAwsSigner({
	config: {
		service: 'mediaconvert',
		region: 'us-east-1',
		accessKeyId: 'AKIAVY******4R5QZ74',
		secretAccessKey: '2GDf***************uaug2NVmne',
	}
});

const request = {
	url: 'https://q25wbt2lc.mediaconvert.us-east-1.amazonaws.com/2017-08-29/jobs',
	method: 'POST',
	path: '/2017-08-29/jobs',
	headers: {
		'Content-Type': 'application/json',		 
		'Host': 'q25wbt2lc.mediaconvert.us-east-1.amazonaws.com',		
	},
	body: {
	  "UserMetadata": {},
	  "Role": "arn:aws:iam::395784617274:role/mediaconvert-role-1",
	  "Settings": {
		"OutputGroups": [
		  {
			"Name": "Apple HLS",
			"Outputs": [           
			  {
				"VideoDescription": {
				  "ScalingBehavior": "DEFAULT",
				  "TimecodeInsertion": "DISABLED",
				  "AntiAlias": "ENABLED",
				  "Sharpness": 50,
				  "CodecSettings": {
					"Codec": "H_264",
					"H264Settings": {
					  "InterlaceMode": "PROGRESSIVE",
					  "NumberReferenceFrames": 3,
					  "Syntax": "DEFAULT",
					  "Softness": 0,
					  "GopClosedCadence": 1,
					  "GopSize": 90,
					  "Slices": 1,
					  "GopBReference": "DISABLED",
					  "SlowPal": "DISABLED",
					  "SpatialAdaptiveQuantization": "ENABLED",
					  "TemporalAdaptiveQuantization": "ENABLED",
					  "FlickerAdaptiveQuantization": "DISABLED",
					  "EntropyEncoding": "CABAC",
					  "Bitrate": 1093000,
					  "FramerateControl": "INITIALIZE_FROM_SOURCE",
					  "RateControlMode": "CBR",
					  "CodecProfile": "MAIN",
					  "Telecine": "NONE",
					  "MinIInterval": 0,
					  "AdaptiveQuantization": "HIGH",
					  "CodecLevel": "AUTO",
					  "FieldEncoding": "PAFF",
					  "SceneChangeDetect": "ENABLED",
					  "QualityTuningLevel": "SINGLE_PASS",
					  "FramerateConversionAlgorithm": "DUPLICATE_DROP",
					  "UnregisteredSeiTimecode": "DISABLED",
					  "GopSizeUnits": "FRAMES",
					  "ParControl": "INITIALIZE_FROM_SOURCE",
					  "NumberBFramesBetweenReferenceFrames": 2,
					  "RepeatPps": "DISABLED"
					}
				  },
				  "AfdSignaling": "NONE",
				  "DropFrameTimecode": "ENABLED",
				  "RespondToAfd": "NONE",
				  "ColorMetadata": "INSERT",
				  "Width": 1080,
				  "Height": 720
				},
				"AudioDescriptions": [
				  {
					"AudioTypeControl": "FOLLOW_INPUT",
					"CodecSettings": {
					  "Codec": "AAC",
					  "AacSettings": {
						"AudioDescriptionBroadcasterMix": "NORMAL",
						"Bitrate": 96000,
						"RateControlMode": "CBR",
						"CodecProfile": "LC",
						"CodingMode": "CODING_MODE_2_0",
						"RawFormat": "NONE",
						"SampleRate": 48000,
						"Specification": "MPEG4"
					  }
					},
					"LanguageCodeControl": "FOLLOW_INPUT",
					"AudioSourceName": "Audio Selector 1"
				  }
				],
				"OutputSettings": {
				  "HlsSettings": {
					"AudioGroupId": "program_audio",
					"AudioRenditionSets": "program_audio",
					"IFrameOnlyManifest": "EXCLUDE"
				  }
				},
				"ContainerSettings": {
				  "Container": "M3U8",
				  "M3u8Settings": {
					"AudioFramesPerPes": 4,
					"PcrControl": "PCR_EVERY_PES_PACKET"					
				  }
				},
				"NameModifier": "_hd"
			  },
			  {
				"VideoDescription": {
				  "ScalingBehavior": "DEFAULT",
				  "TimecodeInsertion": "DISABLED",
				  "AntiAlias": "ENABLED",
				  "Sharpness": 100,
				  "CodecSettings": {
					"Codec": "H_264",
					"H264Settings": {
					  "InterlaceMode": "PROGRESSIVE",
					  "NumberReferenceFrames": 3,
					  "Syntax": "DEFAULT",
					  "Softness": 0,
					  "GopClosedCadence": 1,
					  "GopSize": 90,
					  "Slices": 1,
					  "GopBReference": "DISABLED",
					  "SlowPal": "DISABLED",
					  "SpatialAdaptiveQuantization": "ENABLED",
					  "TemporalAdaptiveQuantization": "ENABLED",
					  "FlickerAdaptiveQuantization": "DISABLED",
					  "EntropyEncoding": "CABAC",
					  "Bitrate": 418000,
					  "FramerateControl": "INITIALIZE_FROM_SOURCE",
					  "RateControlMode": "CBR",
					  "CodecProfile": "MAIN",
					  "Telecine": "NONE",
					  "MinIInterval": 0,
					  "AdaptiveQuantization": "HIGH",
					  "CodecLevel": "LEVEL_3_1",
					  "FieldEncoding": "PAFF",
					  "SceneChangeDetect": "ENABLED",
					  "QualityTuningLevel": "SINGLE_PASS_HQ",
					  "FramerateConversionAlgorithm": "DUPLICATE_DROP",
					  "UnregisteredSeiTimecode": "DISABLED",
					  "GopSizeUnits": "FRAMES",
					  "ParControl": "INITIALIZE_FROM_SOURCE",
					  "NumberBFramesBetweenReferenceFrames": 2,
					  "RepeatPps": "DISABLED"
					}
				  },
				  "AfdSignaling": "NONE",
				  "DropFrameTimecode": "ENABLED",
				  "RespondToAfd": "NONE",
				  "ColorMetadata": "INSERT",
				  "Width": 720,
				  "Height": 480
				},
				"AudioDescriptions": [
				  {
					"AudioTypeControl": "FOLLOW_INPUT",
					"CodecSettings": {
					  "Codec": "AAC",
					  "AacSettings": {
						"AudioDescriptionBroadcasterMix": "NORMAL",
						"Bitrate": 96000,
						"RateControlMode": "CBR",
						"CodecProfile": "LC",
						"CodingMode": "CODING_MODE_2_0",
						"RawFormat": "NONE",
						"SampleRate": 48000,
						"Specification": "MPEG4"
					  }
					},
					"LanguageCodeControl": "FOLLOW_INPUT",
					"AudioSourceName": "Audio Selector 1"
				  }
				],
				"OutputSettings": {
				  "HlsSettings": {
					"AudioGroupId": "program_audio",
					"AudioRenditionSets": "program_audio",
					"IFrameOnlyManifest": "EXCLUDE"
				  }
				},
				"ContainerSettings": {
				  "Container": "M3U8",
				  "M3u8Settings": {
					"AudioFramesPerPes": 4,
					"PcrControl": "PCR_EVERY_PES_PACKET"					
				  }
				},
				"NameModifier": "_sd"
			  }
			],
			"OutputGroupSettings": {
			  "Type": "HLS_GROUP_SETTINGS",
			  "HlsGroupSettings": {
				"ManifestDurationFormat": "INTEGER",
				"SegmentLength": 15,
				"TimedMetadataId3Period": 15,
				"CaptionLanguageSetting": "OMIT",
				"Destination": "s3://my-new-bucket/output_hls/master",
				"TimedMetadataId3Frame": "PRIV",
				"CodecSpecification": "RFC_4281",
				"OutputSelection": "MANIFESTS_AND_SEGMENTS",
				"ProgramDateTimePeriod": 600,
				"MinSegmentLength": 0,
				"DirectoryStructure": "SINGLE_DIRECTORY",
				"ProgramDateTime": "EXCLUDE",
				"SegmentControl": "SINGLE_FILE",
				"ManifestCompression": "NONE",
				"ClientCache": "ENABLED",
				"StreamInfResolution": "INCLUDE"
			  }
			}
		  }
		],
		"AdAvailOffset": 0,
		"Inputs": [
		  {
			"AudioSelectors": {
			   "Audio Selector 1": {
				"DefaultSelection": "DEFAULT",
				"SelectorType": "PID"
			  }
			},
			"VideoSelector": {
			  "ColorSpace": "FOLLOW"
			},
			"FilterEnable": "AUTO",
			"PsiControl": "USE_PSI",
			"FilterStrength": 0,
			"DeblockFilter": "DISABLED",
			"DenoiseFilter": "DISABLED",
			"TimecodeSource": "ZEROBASED",
			"FileInput": "s3://my-new-bucket/input.mp4"
		  }
		]
	  }
	}
};

!(async function(){
	const { authorization, bodyString } = await sign(request)
	 
	console.log(bodyString,authorization,request.headers['X-Amz-Date'])
})()

Now we have new body string, header Authorization and header X-Amz-Date

Leave a Reply