Search code examples
google-cloud-platformoracle11ggoogle-cloud-functionsgoogle-cloud-storage

Cloud function that queries Oracle database


Need some support in building the cloud function that calls Oracle database, wrote the python code and it's on repo and the function calls it with an HTTP trigger, so that's good.

To connect to Oracle, Oracle Client Library is needed, and it's uploaded on cloud storage bucket.

So now the repo and bucket and the function are all set and in the same region, yet the function throws an error that it can't configure the oracle client library

Here is the code if it's important

import cx_Oracle
def queryOracleDatabase(request):
    # Oracle Database Connection
    username = 'x'
    password = 'y'
    connStr = '00.00.00.00:0000/abcd'
    try:
        conn = cx_Oracle.connect(username, password, connStr)
    except cx_Oracle.DatabaseError as e:
        error = e.args
        print('Error: ', error.message)
        return

    # Execute the query
    try:
        cursor = conn.cursor()
        cursor.execute('select * table')
        data = cursor.fetchall()
    except cx_Oracle.DatabaseError as e:
        error = e.args
        print('Error: ', error.message)
        return
    
    # Clean up
    cursor.close()
    conn.close()
    
    return data

And this is the error it throws

Error: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory"

How to connect the function with the bucket?


Solution

  • Given more context provided by the comments, Cloud Functions don't fit so well the use case since they don't provide a persistent disk where you may store your oracle client lib.

    Cloud functions is a very specialized service, such specialization provides a very low adopting curve and makes it the best choice when the use case and tech stack fit such specialization (eg.: No need of FS unless /tmp, or no need to customize the runtime/OS).

    Instead, when the use case does require some degree of customization of the container where the function runs, Cloud Run comes to life. By simply defining a docker container you can make it host the oracle client lib in the FS (everywhere you need), as well as running your function reusing your current code almost as is.

    I presume your teck stack is quite standard, so I would check the docker hub for an image based on python and maybe even the oracle SDK you need.. It would be an easy starting point.

    About accessing the oracle client hosted on a bucket: the cloud function might download it to the /temp storage, but I'm not sure that you can actually load the lib from there. Such approach of storing libraries to buckets is something unusual to me (just personal experience).