Question
· Jan 30

Retrieving Bucket list from S3

Hi Guys,

I'm looking for sample code to connect to S3 and retrieve and backet data? 

Thanks

Product version: Ensemble 2018.1
Discussion (1)2
Log in or sign up to continue

If you use IRIS, the easiest and cleanest way to interact with AWS S3 is by using embedded Python and the boto3 library, which is the official AWS SDK for Python.

IRIS allows you to write class methods with Language = python, so you can write pure Python inside your ObjectScript classes. Here’s a simple example of listing objects in an S3 bucket:

Class MyApp.S3Utils
{

ClassMethod ListS3Objects(bucket As %String = "your-bucket-name") [ Language = python ]
{
    import boto3

    s3 = boto3.client("s3")
    response = s3.list_objects(Bucket=bucket,Prefix="your_folder")

    contents = response.get("Contents", [])
    for obj in contents:
        print("Key:", obj["Key"])
}

}

To call this method from ObjectScript:

Do ##class(MyApp.S3Utils).ListS3Objects("my-s3-bucket")

Just make sure the Python environment IRIS is using has boto3 installed:

pip install boto3

If you’re working with Ensemble, which doesn’t support embedded Python, you can still achieve the same result by calling a Python script externally using $ZF(-1):

Set cmd = "python3 /path/to/your_script.py"
Set status = $ZF(-1, cmd)

This way, you can keep the S3 logic in a Python script and still integrate it with Ensemble.