I am currently developing an OCI Function in Python that receives a PDF file encoded in base64 and converts each page into an individual base64-encoded JPG. This function is executed from an OIC Gen3 Integration.
The function is designed to save these images directly into an Object Storage bucket. However, when the upload process executes, I encounter an error stating that the resource either does not exist or that the function is not authorized to access it. I have manually verified that the bucket exists and the naming is correct.
Below, I am providing my Dynamic Group configuration, the IAM Policies, and the specific code snippet where the put_object call is made.
Does anyone have insight into what might be causing this?
Dynamic Group name= functionsPDFdg
Matching Rules= ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaaaa2f6tuselwfkyovbsn6mbgjkq3uejfruvnsehmhlapu6i5jvr6okq'}
Policy name= Policy_Fn_manage_OS
Statement= Allow dynamic-group functionsPDFdg to manage objects in compartment LACLS
Python:
import io
import json
import base64
import logging
import oci
import uuid
from fdk import response
import pypdfium2 as pdfium
from PIL import Image
logging.basicConfig(level=logging.INFO)
def handler(ctx, data: io.BytesIO = None):
try:
body = data.getvalue()
if not body:
return response.Response(ctx, response_data=json.dumps({"status": "Error", "message": "Cuerpo vacio"}), headers={"Content-Type": "application/json"})
input\_data = json.loads(body)
pdf\_base64 = input\_data.get('pdf\_content\_base64', '').strip()
bucket\_name = input\_data.get('bucket\_name')
namespace = input\_data.get('namespace')
if not all(\[pdf\_base64, bucket\_name, namespace\]):
return response.Response(ctx, response\_data=json.dumps({"status": "Error", "message": "Faltan parametros"}), headers={"Content-Type": "application/json"})
signer = oci.auth.signers.get\_resource\_principals\_signer()
object\_storage\_client = oci.object\_storage.ObjectStorageClient({}, signer=signer)
pdf\_bytes = base64.b64decode(pdf\_base64)
pdf\_document = pdfium.PdfDocument(pdf\_bytes)
num\_pages = len(pdf\_document)
converted\_files = \[\]
for i in range(num\_pages):
page = pdf\_document.get\_page(i)
pil\_image = page.render(scale=3).to\_pil()
img\_byte\_arr = io.BytesIO()
pil\_image.save(img\_byte\_arr, format="JPEG", quality=85)
img\_byte\_arr.seek(0)
# Usamos uuid para evitar errores de InvokeContext
unique\_id = str(uuid.uuid4())\[:8\]
object\_name = f"page\_{i+1}\_{unique\_id}.jpg"
object\_storage\_client.put\_object(namespace, bucket\_name, object\_name, img\_byte\_arr, content\_type="image/jpeg")
converted\_files.append({"page": i + 1, "file\_name": object\_name})
page.close()
pdf\_document.close()
return response.Response(
ctx,
response\_data=json.dumps({"status": "Success", "message": f"Procesadas {num\_pages} paginas", "files": converted\_files}),
headers={"Content-Type": "application/json"}
)
except Exception as e:
logging.error(f"Error: {str(e)}")
return response.Response(
ctx, status_code=500,
response_data=json.dumps({"status": "Error", "message": str(e)}),
headers={"Content-Type": "application/json"}
)