Gitlab使用Token访问请求文件

867 阅读1分钟

项目的目录结构

➜  go_project git:(main) tree
.
├── build
│   └── uat
│       └── deployment.yaml
├── main.go
└── README.md

2 directories, 3 files

获取Access Token

image.png

Gitlab通过url获取文件的url参数

GET /projects/:id/repository/files/:file_path/raw
AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
file_pathstringyesURL-encoded full path to new file, such as lib%2Fclass%2Erb.
refstringnoThe name of branch, tag or commit. Default is the HEAD of the project.
lfsbooleannoDetermines 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

image.png

file_path为文件的路径

如果访问的文件是项目中的文件下的文件的话,那个"/"需要转义成"%2F"或"%2f"

ref为分支名

通过url访问文件

请求头需要加上PRIVATE-TOKEN,值即为上一步生成的token

如下图所示访问的是项目中的build/uat/deployment.yaml文件,所以url需要将"/"转义,如果不转义的话出现404的情况。

image.png

image.png

获取文件的原始格式

请求的url添加上raw以后可以获取文件的原始格式,不加的话如上面的步骤显示的是编码后的内容

image.png