使用以下功能代码:
'use strict';
//import { AWS } from "@aws-sdk/client-s3"
var AWS = require('aws-sdk');
//import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"
var s3 = new AWS.S3({
apiVersion: '2012–09–25'
});
var eltr = new AWS.ElasticTranscoder({
apiVersion: '2012–09–25',
region: 'ap-south-1'
});
exports.handler = function(event, context) {
console.log('Executing Elastic Transcoder');
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var pipelineId = '1****************';
if (bucket !== '*****') {
context.fail('Incorrect Input Bucket');
return;
}
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
//the object may have spaces
var newKey = key.split('.')[0];
var fk= "400K";
var sk= "600K";
var om= "1.5M";
var tm= "2M";
var th= "Thumb"
var params = {
PipelineId: pipelineId,
OutputKeyPrefix: newKey + '/',
Input: {
Key: srcKey,
FrameRate: 'auto',
Resolution: 'auto',
AspectRatio: 'auto',
Interlaced: 'auto',
Container: 'auto'
},
Outputs: [{
Key: fk + '/' +'hls4K-' + newKey + '.ts',
ThumbnailPattern: th +'/'+'thumbs4K-{count}',
PresetId: '***************',//HLS v3 400K/s
SegmentDuration:"5"
},
{
Key: sk + '/' +'hls6K-' + newKey + '.ts',
ThumbnailPattern: th +'/'+'thumbs6K-{count}',
PresetId: '**********', //HLS v3 600K/s
SegmentDuration:"5"
},
{
Key: om + '/' +'hls1.5M-' + newKey + '.ts',
ThumbnailPattern: th +'/'+'thumbs1.5M-{count}',
PresetId: '***************', //HLS v3 1.5M/s
SegmentDuration:"5"
},
,
{
Key: tm + '/' +'hls2M-' + newKey + '.ts',
ThumbnailPattern: th +'/'+'thumbs2M-{count}',
PresetId: '*****************', //HLS v3 2M/s
SegmentDuration:"5"
}
],
Playlists: [{
Format: 'HLSv3',
Name: 'hls-'+ newKey,
OutputKeys: [ fk+ '/' +'hls4K-'+ newKey + '.ts',sk + '/' +'hls6K-' + newKey + '.ts',om + '/'
+'hls1.5M-' + newKey + '.ts',,tm + '/' +'hls2M-' + newKey + '.ts']
}]
};
console.log('Starting Job');
eltr.createJob(params, function(err, data){
if (err){
console.log(err);
} else {
console.log(data);
}
context.succeed('Job well done');
});
};
```
Configuration Template:
```
# This AWS SAM template has been generated from your function's configuration. If
# your function has one or more triggers, note that the AWS resources associated
# with these triggers aren't fully specified in this template and include
# placeholder values. Open this template in AWS Application Composer or your
# favorite IDE and modify it to specify a serverless application with other AWS
# resources.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Application Model template describing your function.
Resources:
videocomporession:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Description: ''
MemorySize: 128
Timeout: 3
Handler: index.handler
Runtime: nodejs20.x
Architectures:
- x86_64
EphemeralStorage:
Size: 512
EventInvokeConfig:
MaximumEventAgeInSeconds: 21600
MaximumRetryAttempts: 2
PackageType: Zip
Policies:
- Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
Resource: arn:aws:logs:us-east-2:158624315941:*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- >-
arn:aws:logs:us-east-2:158624315941:log-group:/aws/lambda/videocomporession:*
SnapStart:
ApplyOn: None
Events:
BucketEvent1:
Type: S3
Properties:
Bucket:
Ref: Bucket1
Events:
- s3:ObjectCreated:*
RuntimeManagementConfig:
UpdateRuntimeOn: Auto
Bucket1:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
BucketPolicy1:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: Bucket1
PolicyDocument:
Statement:
- Action: s3:*
Effect: Deny
Principal: '*'
Resource:
- arn:aws:s3:::Bucket1/*
- arn:aws:s3:::Bucket1
Condition:
Bool:
aws:SecureTransport: false
```
Node.js 18 及更高版本运行时仅包含适用于 JavaScript v3 的 AWS 开发工具包。早期运行时仅包含适用于 JavaScript v2 的 AWS 开发工具包。
我认为你的选择是: