使用 python-gitlab 将图片推送到 gitlab

Pushing picture to gitlab with python-gitlab

提问人:Limone 提问时间:10/30/2023 更新时间:10/30/2023 访问量:23

问:

我正在尝试将 jpg 图片推送到带有 .我的代码如下所示python-gitlab

gl = gitlab.Gitlab(GITLAB_URL, PRIVATE_TOKEN)
project = gl.projects.get(PROJECT_ID)

with open(file_path, 'rb') as f:
  file_content = f.read()

branch = project.branches.get('main')

project.files.create(
  {
    'file_path': f"{REPOSITORY_PATH}/{PATH_IN_REPO}",
    'branch': branch.name,
    'content': file_content,
    'commit_message': commit_message
  }
)

具有(希望)变量的明显含义。我收到错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

我怀疑这与我打开jpg文件的方式有关。提示?

python json utf-8 python-gitlab

评论

0赞 hegerdes 10/30/2023
您正在以二进制模式 ('rb') 读取文件内容,但客户端库需要字符串数据。您应该尝试将其编码为刺痛或以文本模式读取文件
0赞 Limone 10/30/2023
所以像这样的东西,后来在json传递?file_content.encode("base64")encoding: base64

答: 暂无答案