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

how to upload image in bitbucket using rest api call from node.js

Sridhar B
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 13, 2024

 

export const uploadImageToBitbucketServer= async (req, res) => {
const { fileUrl } = req.body;

try {
const fileResponse = await fetch(fileUrl);
const fileBuffer = await fileResponse.buffer();

const fileName = path.basename(fileUrl);
const filePath = `static/img/${fileName}`;
const commitMessage = `Add image ${fileName}`;

const formData = new FormData();
formData.append("message", commitMessage);
formData.append("branch", branchName);
formData.append("file", fileBuffer, { filename: fileName });

const apiEndpoint = `https://api.bitbucket.org/2.0/repositories/${repo}/src`;

const bitbucketResponse = await fetch(apiEndpoint, {
method: "POST",
headers: {
Authorization: `Basic ${Buffer.from(`${bitbucketUsername}:${appPassword}`).toString("base64")}`,
...formData.getHeaders(),
},
body: formData,
});

if (!bitbucketResponse.ok) {
throw new Error(
`Failed to upload image to Bitbucket: ${bitbucketResponse.statusText}`
);
}

const data = await bitbucketResponse.json();
const imageUrl = `https://bitbucket.org/${repo}/raw/${branchName}/static/img/${filePath}`;
res.json({ imageUrl });
} catch (error) {
console.error("Error uploading image:", error);
res.status(500).json({ error: error.message });
}
};

1 answer

0 votes
Sridhar B
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 14, 2024
formData.append("file"fileBuffer, { filename: fileName });
correction  :: replace "file" into filepath
formData.append(filepathfileBuffer, { filename: fileName });

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events