项目的目录结构
➜ go_project git:(main) tree
.
├── build
│ └── uat
│ └── deployment.yaml
├── main.go
└── README.md
2 directories, 3 files
获取Access Token
Gitlab通过url获取文件的url参数
GET /projects/:id/repository/files/:file_path/raw
| Attribute | Type | Required | Description |
|---|---|---|---|
id | integer or string | yes | The ID or URL-encoded path of the project owned by the authenticated user. |
file_path | string | yes | URL-encoded full path to new file, such as lib%2Fclass%2Erb. |
ref | string | no | The name of branch, tag or commit. Default is the HEAD of the project. |
lfs | boolean | no | Determines if the response should be Git LFS file contents, rather than the pointer. If the file is not tracked by Git LFS, ignored. Defaults to false. |
其中id为gitlab中某一个项目的id
file_path为文件的路径
如果访问的文件是项目中的文件下的文件的话,那个"/"需要转义成"%2F"或"%2f"
ref为分支名
通过url访问文件
请求头需要加上PRIVATE-TOKEN,值即为上一步生成的token
如下图所示访问的是项目中的build/uat/deployment.yaml文件,所以url需要将"/"转义,如果不转义的话出现404的情况。
获取文件的原始格式
请求的url添加上raw以后可以获取文件的原始格式,不加的话如上面的步骤显示的是编码后的内容