-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathfunc.py
45 lines (41 loc) · 1.45 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
41
42
43
44
45
#
# oci-objectstorage-put-object 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"]
objectName = body["objectName"]
content = body["content"]
except Exception:
error = """
Input a JSON object in the format: '{"bucketName": "<bucket name>",
"content": "<content>", "objectName": "<object name>"}'
"""
raise Exception(error)
resp = put_object(bucketName, objectName, content)
return response.Response(
ctx,
response_data=json.dumps(resp),
headers={"Content-Type": "application/json"}
)
def put_object(bucketName, objectName, content):
signer = oci.auth.signers.get_resource_principals_signer()
client = oci.object_storage.ObjectStorageClient(config={}, signer=signer)
namespace = client.get_namespace().data
output=""
try:
object = client.put_object(namespace, bucketName, objectName, json.dumps(content))
output = "Success: Put object '" + objectName + "' in bucket '" + bucketName + "'"
except Exception as e:
output = "Failed: " + str(e.message)
return { "state": output }