Created
January 31, 2023 16:04
-
-
Save webgtx/4f2e1ceac7c39765c6899e397b341924 to your computer and use it in GitHub Desktop.
Simple script for handling data in OCI Bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| url = "https://<object-storage-namespace>.compat.objectstorage.<region>.oraclecloud.com/n/<namespace-name>/b/<bucket-name>" | |
| # List objects in the bucket | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| object_list = response.text.split("\n") | |
| print("Objects in the bucket:") | |
| for obj in object_list: | |
| print("- {}".format(obj)) | |
| else: | |
| print("Failed to retrieve object list, status code: {}".format(response.status_code)) | |
| # Upload a file to the bucket | |
| file_path = "<path-to-file>" | |
| file_name = "<object-name>" | |
| with open(file_path, "rb") as file: | |
| headers = { | |
| "Content-Type": "application/octet-stream" | |
| } | |
| upload_url = "{}/{}".format(url, file_name) | |
| response = requests.put(upload_url, headers=headers, data=file) | |
| if response.status_code == 201: | |
| print("File uploaded successfully") | |
| else: | |
| print("Failed to upload file, status code: {}".format(response.status_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment