|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import click |
| 5 | +import requests |
| 6 | +import time |
| 7 | + |
| 8 | +from file_helpers import calculate_md5, encode_base64 |
| 9 | + |
| 10 | +CHUNK_SIZE_BYTES = int(10e6) |
| 11 | + |
| 12 | +class BadRequestException(Exception): |
| 13 | + def __init__(self, message, rv): |
| 14 | + super(BadRequestException, self).__init__(message) |
| 15 | + self.rv = rv |
| 16 | + |
| 17 | + |
| 18 | +def api_post(domain, api_key, path, body): |
| 19 | + url = "https://{}/api/v2/{}".format(domain, path) |
| 20 | + rv = requests.post(url, json=body, auth=(api_key, "")) |
| 21 | + if rv.status_code >= 400: |
| 22 | + raise BadRequestException( |
| 23 | + "Server returned status {}. Response:\n{}".format( |
| 24 | + rv.status_code, json.dumps(rv.json()) |
| 25 | + ), |
| 26 | + rv, |
| 27 | + ) |
| 28 | + return rv.json() |
| 29 | + |
| 30 | + |
| 31 | +@click.command() |
| 32 | +@click.option( |
| 33 | + "--domain", |
| 34 | + help="Domain name of your Benchling instance, e.g. example.benchling.com", |
| 35 | + required=True, |
| 36 | +) |
| 37 | +@click.option("--api-key", help="Your API key", required=True) |
| 38 | +@click.option("--filepath", help="Filepath of blob to upload", required=True) |
| 39 | +@click.option("--destination-filename", help="Name of file (omit to keep same name as source)", required=False) |
| 40 | +def main( |
| 41 | + domain, |
| 42 | + api_key, |
| 43 | + filepath, |
| 44 | + destination_filename, |
| 45 | +): |
| 46 | + name = destination_filename |
| 47 | + if name is None: |
| 48 | + name = os.path.basename(filepath) |
| 49 | + file_size = os.path.getsize(filepath) |
| 50 | + with open(filepath, "rb") as file: |
| 51 | + if file_size <= CHUNK_SIZE_BYTES: |
| 52 | + upload_single_part_blob(api_key, domain, file, name) |
| 53 | + else: |
| 54 | + upload_multi_part_blob(api_key, domain, file, name) |
| 55 | + |
| 56 | + |
| 57 | +def upload_single_part_blob(api_key, domain, file, name): |
| 58 | + file_contents = file.read() |
| 59 | + encoded64 = encode_base64(file_contents) |
| 60 | + md5 = calculate_md5(file_contents) |
| 61 | + res = api_post(domain, api_key, "blobs", { |
| 62 | + "data64": encoded64, |
| 63 | + "md5": md5, |
| 64 | + "mimeType": "application/octet-stream", |
| 65 | + "name": name, |
| 66 | + "type": "RAW_FILE", |
| 67 | + }) |
| 68 | + assert(res["uploadStatus"] == "COMPLETE") |
| 69 | + print("Finished uploading {} with blob ID {}".format( |
| 70 | + res["name"], res["id"] |
| 71 | + )) |
| 72 | + |
| 73 | + |
| 74 | +def upload_multi_part_blob(api_key, domain, file, name): |
| 75 | + chunk_producer = lambda chunk_size: file.read(chunk_size) |
| 76 | + start_blob = api_post(domain, api_key, "blobs:start-multipart-upload", { |
| 77 | + "mimeType": "application/octet-stream", |
| 78 | + "name": name, |
| 79 | + "type": "RAW_FILE", |
| 80 | + }) |
| 81 | + part_number = 0 |
| 82 | + blob_parts = [] |
| 83 | + try: |
| 84 | + while True: |
| 85 | + cursor = chunk_producer(CHUNK_SIZE_BYTES) |
| 86 | + if not cursor: |
| 87 | + break |
| 88 | + part_number += 1 |
| 89 | + encoded64 = encode_base64(cursor) |
| 90 | + md5 = calculate_md5(cursor) |
| 91 | + created_part = api_post(domain, api_key, "blobs/{}/parts".format(start_blob["id"]), { |
| 92 | + "data64": encoded64, |
| 93 | + "md5": md5, |
| 94 | + "partNumber": part_number, |
| 95 | + }) |
| 96 | + blob_parts.append(created_part) |
| 97 | + api_post(domain, api_key, "blobs/{}:complete-upload".format(start_blob["id"]), { |
| 98 | + "parts": blob_parts |
| 99 | + }) |
| 100 | + print("Completed uploading {} parts for blob {}".format(part_number, start_blob["id"])) |
| 101 | + except Exception as e: |
| 102 | + print("Error while uploading part {} for blob {}".format(part_number, start_blob["id"])) |
| 103 | + api_post(domain, api_key, "blobs/{}:abort-upload".format(start_blob["id"]), {}) |
| 104 | + raise e |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments