How to get a file from CodeCommit using JS AWS-SDK

0

Hello, is there a way to read a file from a specific branch in a CodeCommit repository using the aws-sdk for js/node?

I need to do this from a lambda function.

I saw there's a getFile method but the docs lacks of examples. What I've done so far:

const client = new AWS.CodeCommit({ region: "us-east-1" });

    const file = await client.getFile({
        filePath: "myFile.txt",
        repositoryName: "myRepo",
        commitSpecifier: "myBranch"
    });

the documentation says that getFile Returns the base-64 encoded contents of a specified file and its metadata. But is also says the return type is a AWS.Request object, so I can I read it?

thanks M

已提問 1 年前檢視次數 633 次
2 個答案
2

After strugling half a day I found a solution I'd like to share:

function uint8arrayToStringMethod(myUint8Arr) {
    return String.fromCharCode.apply(null, myUint8Arr);
}

const client = new AWS.CodeCommit({ region: "us-east-1" });
const config = {
        filePath: "myFile.txt",
        repositoryName: "myRepo",
        commitSpecifier: "myBranch"
};
const file = await client.getFile(config);
const u8str = uint8arrayToStringMethod(file.fileContent);

I am using aws-sdk V3

thanks everyone for your help. M

已回答 1 年前
0

Hi there,

Using the github search feature I was able to find an example on how the read the file contents:

Example use of getFile

export async function getRawFile(
  fileName: string,
  repoName?: string,
  branchOrTag?: string
): Promise<string | null> {
  const fileRes = await client.getFile(
    repoName ?? config.repository,
    fileName,
    branchOrTag
  );
  if (!fileRes?.fileContent) {
    return null;
  }
  const buf = Buffer.from(fileRes.fileContent);
  return buf.toString();
}

The commitSpecifier seems to have to be a commitId in stead of a branchName though.

Regards, Jacco

profile picture
JaccoPK
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南