0
votes

I'm new to nodeJS and Currently implementing a copy end point to copy files in S3 bucket to another using nodeJS. So the Lambda function that I need to write, needs to copy .m3u8 files and Its corresponding .ts files to another bucket while providing different name to the .m3u8 file. If the naming format of the source .m3u8 file is ${mediaId1}.m3u8 then the copied .m3u8 file should be ${mediaId2}.m3u8. The source .m3u8 and its corresponding .ts files are in the same bucket and the .ts files and fellow .m3u8 files don't need to change its name while coping, so only the main .m3u8 needs to get copied while changing its name.

I tried coping .m3u8 file as follows.

const SourceKey = `${result.Item.system}/${result.Item.prefix}/${result.Item.mediaId}/AppleHLS1/${result.Item.mediaId}.m3u8`;
           const TargetKey = `${result.Item.system}/${data.prefix}/${targertmediaId}/AppleHLS1/${targertmediaId}.m3u8`;
           var paramsS3 = {
             Bucket: process.env.S3_BUCKET_NAME_OUTPUT,
             Key: TargetKey,
             Metadata: data.metadata,
             SourceKey: SourceKey,
           };
 
 
           s3.copyObject(paramsS3, function (error, data) {
             if (error) console.log(error, error.stack);
             else  console.log(data)
               
           });

So Is there any possible way that I can list the remaining .ts and .m3u8 files in the bucket and copy them one by one, without coping each at once.

Here is the bucket that all the .ts and .m3u8 files are located. Only the last .m3u8 file need to change its name while other .m3u8 and .ts remaining same while coping. enter image description here

enter image description here

1

1 Answers

0
votes

You can use the ListObjectsCommand command available in the s3 client to list all the objects inside a bucket. You can specify the prefix of your objects for filtering results.

In your case, you may have to prepare two ListObjectsCommand, one for the main .m3u8 file and one for all others.

You can then use the PutObjectCommand to copy the files one by one.

Check the Developer Guide