Python Lib for Oracle Cloud Object Storage Classic

Do you like working with REST APIs? Do you know Oracle Cloud Infrastructure Object Storage Classic? There are many REST APIs that you can use within your application to integrate with the Oracle Cloud Infrastructure Object Storage Classic. In addition to Object Storage Classic, all Oracle Cloud solutions have REST APIs!

In this post, you will learn how to use the Oracle Public Cloud (OPC) Python Lib that I created using the REST APIs for Standard Storage in Oracle Cloud Infrastructure Object Storage Classic to facilitate Python development with the Oracle Cloud.

Download the latest version of OPC Lib here: https://github.com/waslleysouza/opc_ocic.git.

I made some modifications in the Face Recognition application (Face Recognition in Oracle Cloud) to demonstrate how to use my OPC Lib.

I added the function “update_known_faces” to download all pictures from container “uploads” in the cloud.

def update_known_faces():
    print("Getting known faces from cloud container...")
    _, files = opc_storage.show_container_details_and_list_objects(UPLOAD_FOLDER)

    for filename in files:
        print("Getting file " + filename + "...")
        _, file = opc_storage.get_object_content_and_metadata(UPLOAD_FOLDER, filename)
        im = Image.open(BytesIO(file))
        im.save(os.path.join(UPLOAD_FOLDER, filename))

I modified the function “upload_file” to upload new pictures to container “uploads” in the cloud.

# Upload to cloud container
image_bytes = pil_image.tobytes(get_content_type(file), 'RGB')
opc_storage.create_or_replace_object(UPLOAD_FOLDER, filename, image_bytes)

I modified the function “main” to instantiate the OPC Lib, to create the container “uploads” in the cloud, and to download all the pictures from container “uploads” in the cloud to folder “uploads” in the application.

if __name__ == "__main__":
    opc_storage = opc.Storage("my-username", "my-password", "my-identity-domain")
    opc_storage.create_container(UPLOAD_FOLDER)

    update_known_faces()
    cache_known_faces()

    app.run(host='0.0.0.0', port=os.environ.get('PORT', 5000))

When you start your application for the first time, the container “uploads” in the cloud is created.

Try to upload a new picture.

Your picture will be uploaded to the container “uploads” in the cloud.

Go to folder “uploads” in the application and remove all your pictures.

Stop and start again your application.
When you start your application for the second time, all the pictures from container “uploads” in the cloud will be downloaded to update your folder “uploads” in the application.

Have a good time!

Author: Waslley Souza

Consultor Oracle com foco em tecnologias Oracle Fusion Middleware e SOA. Certificado Oracle WebCenter Portal, Oracle ADF e Java.

Leave a Reply

Your email address will not be published. Required fields are marked *