|
| 1 | +import os |
| 2 | +import oci |
| 3 | +import sys |
| 4 | +import time |
| 5 | +from oci.object_storage import UploadManager |
| 6 | +from oci.object_storage.transfer.constants import MEBIBYTE |
| 7 | +from oci.object_storage.transfer.internal.download.DownloadConfiguration import DownloadConfiguration |
| 8 | +from oci.object_storage.transfer.internal.download.DownloadManager import DownloadManager |
| 9 | +from oci.object_storage.transfer.internal.download.DownloadThread import DownloadState |
| 10 | +from oci.retry.retry import NoneRetryStrategy, BACKOFF_FULL_JITTER_VALUE |
| 11 | +from oci.exceptions import ResumableDownloadException, DownloadTerminated |
| 12 | + |
| 13 | + |
| 14 | +def upload_progress_callback(bytes_uploaded): |
| 15 | + print(f"{bytes_uploaded} additional bytes uploaded") |
| 16 | + |
| 17 | + |
| 18 | +def on_press(key, download_manager): |
| 19 | + try: |
| 20 | + if key.char == 'p': |
| 21 | + if download_manager.state == 0: |
| 22 | + print("Download Paused!\n") |
| 23 | + download_manager.state = 1 |
| 24 | + return |
| 25 | + |
| 26 | + if download_manager.state == 1: |
| 27 | + print("Download Resumed!\n") |
| 28 | + download_manager.state = 0 |
| 29 | + return |
| 30 | + |
| 31 | + if key.char == 'q': |
| 32 | + print("Terminating Download!\n") |
| 33 | + download_manager.state = -1 |
| 34 | + return |
| 35 | + |
| 36 | + except AttributeError: |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def print_progress_bar(downloaded_bytes, total_bytes, bar_length=60): |
| 41 | + if (total_bytes == 0): |
| 42 | + print(f"Starting Download\n[{'#' * 0 + '-' * (bar_length - 0)}] {0 * 100:.2f}%") |
| 43 | + else: |
| 44 | + progress = downloaded_bytes / total_bytes |
| 45 | + block = int(round(bar_length * progress)) |
| 46 | + progress_bar = f"[{'#' * block + '-' * (bar_length - block)}] {progress * 100:.2f}%" |
| 47 | + |
| 48 | + CURSOR_UP_ONE = '\x1b[1A' |
| 49 | + ERASE_LINE = '\x1b[2K' |
| 50 | + sys.stdout.write(CURSOR_UP_ONE) |
| 51 | + sys.stdout.write(ERASE_LINE) |
| 52 | + sys.stdout.write(CURSOR_UP_ONE) |
| 53 | + sys.stdout.write(ERASE_LINE) |
| 54 | + print(f"Downloading: {downloaded_bytes}/{total_bytes} bytes\n{progress_bar}") |
| 55 | + sys.stdout.flush() |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + """ |
| 60 | + This example program works as follows. |
| 61 | + 1) First we upload an object to an existing bucket. |
| 62 | + 2) Then we download it. |
| 63 | + 3) While the download is in progress, press p on the keyboard to pause the download and p again to resume, q to |
| 64 | + cancel the download. |
| 65 | + # Please note that while the download is in progress don't type else where as the key presses will be registered as |
| 66 | + pause or terminate signals. |
| 67 | +
|
| 68 | + Below are the parameters that are to be filled. Running this program in the terminal is good option as the download |
| 69 | + progress is seen better that way. |
| 70 | +
|
| 71 | + PLEASE NOTE THAT THIS EXAMPLE USES pynput (pip install pynput) so it is advisable to re install all packages in the |
| 72 | + requirements.txt. |
| 73 | +
|
| 74 | + Use `diff uploaded_file_name downloaded_file_name` in the terminal to check if the uploaded and downloaded files are |
| 75 | + the same. |
| 76 | +
|
| 77 | + The Progress call back can add significant delays to the overall download speed. |
| 78 | +
|
| 79 | + Fill the following parameters: |
| 80 | + """ |
| 81 | + namespace = "bm2ywytyr7us" # Put your namespace |
| 82 | + bucket_name = "bucket-20240527-1635" # Put your bucket name |
| 83 | + object_name = "11MB_dummy.txt" # Put your object name or leave it as it is. |
| 84 | + upload_filename = r'11MB_dummy.txt' |
| 85 | + download2path = r'down_11MB_dummy.txt' |
| 86 | + file_size_in_mebibytes = 11 |
| 87 | + part_size = int(1 * MEBIBYTE) |
| 88 | + num_connections = 4 |
| 89 | + |
| 90 | + print(f"Part size is {part_size} MB") |
| 91 | + print(f"Number of connections is {num_connections}") |
| 92 | + |
| 93 | + object_storage_client_config = oci.config.from_file() |
| 94 | + |
| 95 | + compartment_id = object_storage_client_config["tenancy"] |
| 96 | + object_storage = oci.object_storage.ObjectStorageClient(object_storage_client_config) |
| 97 | + |
| 98 | + # create example file to upload |
| 99 | + sample_content = b'a' |
| 100 | + with open(upload_filename, 'wb') as f: |
| 101 | + while f.tell() < MEBIBYTE * file_size_in_mebibytes: |
| 102 | + f.write(sample_content * MEBIBYTE) |
| 103 | + |
| 104 | + print(f"Uploading new object {object_name!r}") |
| 105 | + |
| 106 | + start_time = time.time() |
| 107 | + upload_manager = UploadManager(object_storage, allow_parallel_uploads=True, parallel_process_count=num_connections) |
| 108 | + response = upload_manager.upload_file( |
| 109 | + namespace, bucket_name, object_name, upload_filename, part_size=part_size) |
| 110 | + |
| 111 | + end_time = time.time() |
| 112 | + print("Time taken to upload file: ", end_time - start_time) |
| 113 | + |
| 114 | + download_configuration = DownloadConfiguration(allow_multipart=True, num_download_threads=num_connections, |
| 115 | + part_size_in_bytes=part_size, max_retries=1, |
| 116 | + backoff_type=BACKOFF_FULL_JITTER_VALUE) |
| 117 | + |
| 118 | + download_manager = DownloadManager(download_configuration, object_storage, DownloadState.DOWNLOADING) |
| 119 | + |
| 120 | + print_progress_bar(0, 0) |
| 121 | + |
| 122 | + start = time.time() |
| 123 | + retry_strategy = NoneRetryStrategy() |
| 124 | + try: |
| 125 | + bytes_written, down_response = download_manager.get_object_to_path(namespace, bucket_name, object_name, |
| 126 | + download2path, |
| 127 | + progress_callback=print_progress_bar) |
| 128 | + end = time.time() |
| 129 | + print("--------------------------------------------------------") |
| 130 | + print("Download Headers:\n", down_response.headers) |
| 131 | + print("Download took {} seconds".format(end - start)) |
| 132 | + os.remove(upload_filename) |
| 133 | + os.remove(download2path) |
| 134 | + |
| 135 | + except Exception as e: |
| 136 | + exception = e |
| 137 | + while True: |
| 138 | + if isinstance(exception, ResumableDownloadException): |
| 139 | + print("Failed parts: ", exception.failed_parts) |
| 140 | + ans = (input( |
| 141 | + "Retry the failed parts parallely using resume_parts_to_path_multithread() method? [y/n] ").strip().lower()) |
| 142 | + |
| 143 | + if ans == "y": |
| 144 | + try: |
| 145 | + _ = download_manager.resume_parts_to_path_multithread(namespace, bucket_name, object_name, |
| 146 | + list(exception.failed_parts.keys()), |
| 147 | + download2path) |
| 148 | + os.remove(upload_filename) |
| 149 | + os.remove(download2path) |
| 150 | + break # Exit the loop after retrying |
| 151 | + except ResumableDownloadException as e: |
| 152 | + print("ResumableDownloadException occurred again.") |
| 153 | + exception = e |
| 154 | + continue |
| 155 | + except Exception as retry_exception: |
| 156 | + raise retry_exception |
| 157 | + # Exit the loop if an unexpected exception occurs during retry |
| 158 | + else: |
| 159 | + break # Exit the loop if the user chooses not to retry |
| 160 | + elif isinstance(exception, DownloadTerminated): |
| 161 | + print("Download was terminated") |
| 162 | + break |
| 163 | + else: |
| 164 | + raise exception |
0 commit comments