Home > OS >  Can CodeBuild can access object metadata inside codepipeline?
Can CodeBuild can access object metadata inside codepipeline?

Time:01-20

I am trying to access S3 objects user-defined metadata inside codebuild and set as environment variable.

As per docs, it only output etag and VersionId. So I am assuming by default user defined metadata is not exported to codepipeline when s3 is a source action

I am thinking to use aws cli command and then set this as environment variable for the codebuild. Is there a better way?

aws s3api head-object --bucket bucket-name --profile profile --key xxxx.zip

CodePudding user response:

You are right the only way to get the object metadata is to use head-object CLI call. You can use the below buildspec in your CodeBuild stage to get the object metadata for a pipeline with s3 source action.

version: 0.2

phases:
  build:
    commands:
      - BUCKET_PATH=$(echo $CODEBUILD_SOURCE_VERSION | cut -d ':' -f 6)
      - BUCKET=$(echo $BUCKET_PATH | cut -d '/' -f 1)
      - KEY=$(echo $BUCKET_PATH | cut -d '/' -f 2,3,4)
      - aws s3api head-object --bucket $BUCKET --key $KEY --query Metadata

Please note that updating metadata on the s3 source object will also trigger the pipeline with s3 source action.

CodePudding user response:

CodeBuild can access object metadata inside CodePipeline by using the AWS CodePipeline API. CodeBuild can use this API to retrieve information about the current state of a pipeline, including information about the source and output artifacts of each stage. To access the metadata of an object in CodePipeline, you will need to use the "GetPipelineExecution" API action and include the "metadata" parameter in the request. This will return the metadata for each artifact in the pipeline execution.

  • Related