Deepbits Platform: Upload a file

This API endpoint (POST /sbom_builder/upload_url) allows users to upload a file to the Deepbits platform. Users specify the file to upload (file_path) and provide a description (file_description) along with their API key (x-api-key header) for authentication.

Upon successful file upload, the API returns a JSON response (200 OK) containing the upload URL and other metadata necessary for the upload process. The file is uploaded to the specified URL using PUT method with the file contents (file_contents) and appropriate headers.

Below is a demo script demonstrating how to upload a file to the Deepbits Platform. Adjust the placeholders , , , and with your specific values for integration.

import requests
import hashlib
import os

API_KEY = '<Your API key generated from app.deepbits.com>'
API_BASE = 'https://api.deepbits.com/api/v1'

default_headers = {"x-api-key": API_KEY }

FILE_TO_UPLOAD = '<Your file path>'
FILE_TYPE = '<Your file type>' # type could be meta | binary | repo | sbom
RESULT_JSON = '<Your result json path>'

def main():
    response = upload_file(FILE_TO_UPLOAD, "file_description", API_KEY)
    print('Uploaded File ID:', response.get('data', {}).get('_id'))

def upload_file(file_path, file_description, api_key):
    try:
        with open(file_path, "rb") as f:
            file_contents = f.read()

        print(f"Get upload URL for {os.path.basename(file_path)}...")
        file_name = os.path.basename(file_path)
        upload_url_response = requests.post(
            f"{API_BASE}/sbom_builder/upload_url",
            json={"fileName": file_name},
            headers=default_headers
        )
        upload_url_response.raise_for_status()
        print(f"Upload URL Response: {upload_url_response.json()}\n")

        if upload_url_response.json().get('meta', {}).get('code') == 403:
            print("API key is invalid")
            raise Exception("API key is invalid")

        put_url = upload_url_response.json().get('data', {}).get('uploadUrl')

        headers = {
            "Content-Type": "application/octet-stream",
            "x-api-key": api_key,
        }

        print(f"Upload {os.path.basename(file_path)} to S3...")
        put_response = requests.put(
            put_url,
            data=file_contents,
            headers=headers
        )
        put_response.raise_for_status()

        print(f"Trigger upload_success...")
        upload_success_response = requests.put(
            f"{API_BASE}/sbom_builder/upload_success",
            json={
                "path": upload_url_response.json().get('data', {}).get('path'),
                "hash": hashlib.sha256(file_contents).hexdigest(),
                "fileName": os.path.basename(file_path),
                "desc": file_description,
                "selectedType": FILE_TYPE
            },
            headers=default_headers
        )
        upload_success_response.raise_for_status()

        print(f"Upload Success Response: {upload_success_response.json()}\n")
        return upload_success_response.json()

    except requests.RequestException as e:
        print(f"An error occurred with the HTTP request: {e}")
        return None
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

if __name__ == "__main__":
    main()