javascriptnode.jsamazon-s3brightcove

Nodejs - Upload file to AWS W3 with signed url generated with Brightcove returns 403 Forbidden


I'm trying to add a video to Brightcove following steps in this article https://apis.support.brightcove.com/dynamic-ingest/getting-started/source-file-upload-api-dynamic-ingest.html

The first 2 steps work fine. After requesting the S3 URLS, I get a response like this

    {
      "bucket": "ingestion-upload-production",
      "object_key": "57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4",
      "access_key_id": "ACCESS_KEY_APPEARS_HERE",
      "secret_access_key": "SECRET_ACCESS_KEY_APPEARS_HERE",
      "session_token": "FQoDYXdzEKf//////////wEaDKR0wDgquq/qvkZgbyKOA7URC/9io6cmRBDkhbvxoHIKkPZlK/9YNvdWcESPkm75/2PvU6FV1Mc+/XENPzY8KgvP86MBJNxYLPdkuP1phgHs2Yh2p1KIDcQSCZJ3i6i9m4S14ewjWIugYLYDQi6CG+3fiFwfzbKT5jes1kh24m9BQQIuvVOiM1GLTldyDzlrdDopJkdYd4IEU7FU36CUT7RL/aeMwR2Usk56nwqyqkkQHPmvqmGyiLdrD3OrIbUU+6+ZP4usS9dbV3eAqOWDIk3HCN+Kuc9f/eUWhY21ftNDXWgasqQqXwPRs3T1i/hoiIKODbzr8F",
      "signed_url": "https://ingestion-upload-production.s3.amazonaws.com/57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4?AWSAccessKeyId=ACCESS_KEY_HERE&Expires=1475673952&Signature=%2Fsr5cV%2FVOfGCBkodol9xQIKlbu4%3D",
      "api_request_url": "https://ingestion-upload-production.s3.amazonaws.com/57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4"
}

The article only mention the way to put the file to S3 using the curl command, which works when executed in terminal

curl -X PUT "SIGNED_URL_GOES_HERE" --upload-file FILE_PATH_FOR_LOCAL_ASSET_GOES_HERE 

I tried to implement this in nodejs, but that wasn't successful

  1. Using node-fetch & PUT method
        fetch(signedUrl, {
        method: 'PUT',
        data: {
            'value': fs.createReadStream(file_path),
            'options': {
              'filename': file_name,
              'contentType': file_type
            }
        },

        **OR**
        data : fs.createReadStream(file_path)

        **OR**
        data : fs.readFileSync(file_path),

        headers:{
            "Content-Type":file_type
        }
    })

This returns 403 Forbidden

  1. So I tested it with Postman

PUT signedUrl

Body binary (not form-data)

I got as response:

    <?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
    <AWSAccessKeyId>AWSAccessKeyId</AWSAccessKeyId>
    <StringToSign>PUT

video/quicktime
1594858748
/<BUCKET>/<KEY></StringToSign>
    <SignatureProvided><SIGNATURE>=</SignatureProvided>
    <StringToSignBytes>50 55 54 0a 0a 76 69 64 65 6f 2f 71 75 69 63 6b 74 69 6d 65 0a 31 35 39 34 38 35 38 37 34 38 0a 2f 69 6e 67 65 73 74 69 6f 6e 2d 75 70 6c 6f 61 64 2d 70 72 6f 64 75 63 74 69 6f 6e 2f 33 38 34 35 62 2d 39 63 39 64 2d 61 65 31 63 32 39 30 65 30 63 36 64 2f 74 65 73 74 2d 6d 65 64 65 6f 2e 6d 6f 76</StringToSignBytes>
    <RequestId></RequestId>
    <HostId></HostId>
</Error>

I tried other ways like:

const command = `curl -X PUT ${signedUrl} --upload-file ${filepath}`;
execSync(command).toString();

I get --upload-file: command not found

const AWS = require('aws-sdk');
const config = {
            accessKeyId: ACCESS_KEY_ID,
            secretAccessKey: SECRET_ACCESS_KEY
        };
        AWS.config.update(config);

        const S3Bucket = new AWS.S3({
            params: { Bucket: response.BUCKET },
        });

        var params = {
            Key: KEY,
            Body: fs.createReadStream(file_name)
        };
        S3Bucket.upload(params, function (err, data) {
            if (err) {
                console.log('ERROR MSG: ', err);
            } else {
                console.log('Successfully uploaded data');
            }
        });

or even

AWS.config.update({
            accessKeyId: access_key_id,
            secretAccessKey: secret_access_key
        });
        
        let params = {
            Bucket: bucket,
            Key: key,
            Body: fs.createReadStream(filename)
        };
        try {
            let uploadPromise = await new AWS.S3().putObject(params).promise();
            console.log("Successfully uploaded data to bucket", uploadPromise);
        } catch (e) {
            console.log("Error uploading data: ", e);
        } 

What am i doing wrong? I tested all the solutions provided here stackoverflow with no success Could you please share a working code to put file to AWS S3 using nodejs and a pre signed url

Thank you in advance


Solution

  • Based on the documentation here https://apis.support.brightcove.com/dynamic-ingest/getting-started/source-file-upload-api-dynamic-ingest.html#Authentication

    (the PHP example)

    You can do it like this

    const response = {
          "bucket": "ingestion-upload-production",
          "object_key": "57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4",
          "access_key_id": "ACCESS_KEY_APPEARS_HERE",
          "secret_access_key": "SECRET_ACCESS_KEY_APPEARS_HERE",
          "session_token": "FQoDYXdzEKf//////////wEaDKR0wDgquq/qvkZgbyKOA7URC/9io6cmRBDkhbvxoHIKkPZlK/9YNvdWcESPkm75/2PvU6FV1Mc+/XENPzY8KgvP86MBJNxYLPdkuP1phgHs2Yh2p1KIDcQSCZJ3i6i9m4S14ewjWIugYLYDQi6CG+3fiFwfzbKT5jes1kh24m9BQQIuvVOiM1GLTldyDzlrdDopJkdYd4IEU7FU36CUT7RL/aeMwR2Usk56nwqyqkkQHPmvqmGyiLdrD3OrIbUU+6+ZP4usS9dbV3eAqOWDIk3HCN+Kuc9f/eUWhY21ftNDXWgasqQqXwPRs3T1i/hoiIKODbzr8F",
          "signed_url": "https://ingestion-upload-production.s3.amazonaws.com/57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4?AWSAccessKeyId=ACCESS_KEY_HERE&Expires=1475673952&Signature=%2Fsr5cV%2FVOfGCBkodol9xQIKlbu4%3D",
          "api_request_url": "https://ingestion-upload-production.s3.amazonaws.com/57838016001/4752143002001/ed5a5ba0-1d97-4f95-a8ec-cbb786b04a37/greatblueheron.mp4"
    }
    const s3 = new S3({
      region: 'us-east-1',
      credentials: {
        accessKeyId: response.access_key_id,
        secretAccessKey: response.secret_access_key,
        sessionToken: response.session_token
      }
    })
    s3.upload({
      Bucket: response.bucket,
      Key: response.object_key,
      Body: fs.createReadStream(file_name)
    })