forked from oracle-samples/oracle-functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc.py
40 lines (34 loc) · 1.24 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#
# oci-objectstorage-list-objects-python version 1.0.
#
# Copyright (c) 2020 Oracle, Inc.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
#
import io
import os
import json
import sys
from fdk import response
import oci.object_storage
def handler(ctx, data: io.BytesIO=None):
try:
body = json.loads(data.getvalue())
bucketName = body["bucketName"]
except Exception:
raise Exception('Input a JSON object in the format: \'{"bucketName": "<bucket name>"}\' ')
resp = list_objects(bucketName)
return response.Response(
ctx,
response_data=json.dumps(resp),
headers={"Content-Type": "application/json"}
)
def list_objects(bucketName):
signer = oci.auth.signers.get_resource_principals_signer()
client = oci.object_storage.ObjectStorageClient(config={}, signer=signer)
namespace = client.get_namespace().data
print("Searching for objects in bucket " + bucketName, file=sys.stderr)
object = client.list_objects(namespace, bucketName)
print("found objects", flush=True)
objects = [b.name for b in object.data.objects]
response = { "Objects found in bucket '" + bucketName + "'": objects }
return response