4

I`m trying to upload image in a project in gitlab. According to documentation it should not be a rocket science. I have tried to pass the image as url and base-64 representation. If I encode the base-64 URL there is a little progress - internal server error. Any ideas? Thanks!

Community
  • 1
  • 1
Tokotome
  • 91
  • 2
  • 10
  • Did you ever get an answer to your question? I'm running into the same issue. – Dan Aug 15 '17 at 22:49
  • Note "Uploads a file to the specified project to be used in an issue or merge request description, or a comment." This is not the same as uploading to the repository – Jay Wick Sep 04 '19 at 07:35

2 Answers2

0

Since you don't mentioned how you want upload..

With curl:

curl --request POST --header "PRIVATE-TOKEN: XXXXXXXXXX" --form "file=@dk.png" https://gitlab.example.com/api/v4/projects/5/uploads

5 is the id of your project.

gitlab api doc

Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
0

According to this post : Encoding an image file with base64

we could understand how to convert binary files to a base64 string including image, zip or pickle etc. This is apart of my code. Hope it is helpful.

import gitlab
def test_upload_img_to_repo():
    
    gitlab.Gitlab(url=HOST, private_token=PERSONAL_TOKEN)
    project = gl.projects.get(project_id)
    file_path = path.join(path.dirname(__file__), 'test_upload.txt')
    img_path  = path.join(path.dirname(__file__), 'logo.png')
    data = {
        'branch': 'master',
        'commit_message': 'blah blah blah',
        'actions': [
            {
                'action': 'update',
                'file_path': 'upload.txt',
                'content': open(file_path).read(),
            },
            {
                # Binary files need to be base64 encoded
                'action': 'create',
                'file_path': '/folder/logo.png',
                'content': base64.b64encode(open(img_path,'rb').read()).decode(),
                'encoding': 'base64',
            }
        ]
    }

    commit = project.commits.create(data)
    print(commit)