Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to commit multiple files from memory using bitbucket API?

Kurt Rhee October 26, 2021

Hello I have been able to upload files to bitbucket cloud by saving them first to my disk and then commiting them one by one to my repository via the API, but I haven't been able to send files that are in memory and I haven't been able to send multiple files which should get committed to different folders at the same time.

Does anybody know how to do this?

I am writing my api functions in python using the requests library.

1 answer

0 votes
Syahrul
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 23, 2021

Hi @Kurt Rhee 

Welcome to the community!

If I got this right you wish to use the API to create commit on multiple files from memory? If yes, I believe the following curl API endpoint can be used with the content type of multipart/form-data to commit multiple files:

curl --request POST \
  --url https://api.bitbucket.org/2.0/repositories/<WorkspaceID>/<RepoSlug>/src \
  --header 'Authorization: Basic cm9.....==' \
  --header 'Content-Type: multipart/form-data' \
  --form 'message=my commit message' \
  --form branch=<branch name> \
  --form file1=@<My First File Path>\
  --form directory/file2=@<My Second File Path>


As for using python, I am not an expert on this, but from my research on how to POST a file with Python and I found the following example:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder(
    fields={
            'branch': 'dev', 
            'message': '<Your MESSAGE>',
            '/path/file1': ('filename', open('file1.py', 'rb'), 'text/plain'),
            '/path/file2': ('filename', open('file2.py', 'rb'), 'text/plain'),}
    )
....
....
r = requests.post(url, data=m, headers)

I hope this helps!

Cheers,
Syahrul

Kurt Rhee November 24, 2021

Hello this is very helpful thank you!

Like Syahrul likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events