-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathfunc.py
More file actions
52 lines (46 loc) · 1.75 KB
/
func.py
File metadata and controls
52 lines (46 loc) · 1.75 KB
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
46
47
48
49
50
51
52
#
# oci-objectstorage-onsr-put-object-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
# Certs are mounted at this location for ONSR realms
cert_file_path = "/etc/oci-pki/customer/customer-cert.pem"
# file to upload
file_to_upload = "onsr_cert_test"
file_to_upload_content = {"content":"This is test file for ONSR test"}
def handler(ctx, data: io.BytesIO=None):
try:
body = json.loads(data.getvalue())
bucketName = body["bucketName"]
except Exception:
error = """
Input a JSON object in the format: '{"bucketName": "<bucket name>",
"content": "<content>", "objectName": "<object name>"}'
"""
raise Exception(error)
signer = oci.auth.signers.get_resource_principals_signer()
client = oci.object_storage.ObjectStorageClient(config={}, signer=signer)
if os.path.exists(cert_file_path):
client.base_client.session.verify = cert_file_path
resp = put_object(client, bucketName, file_to_upload, file_to_upload_content)
return response.Response(
ctx,
response_data=json.dumps(resp),
headers={"Content-Type": "application/json"}
)
def put_object(client, bucketName, objectName, content):
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 }