oracle / oci-typescript-sdk

Oracle Cloud Infrastructure SDK for TypeScript and JavaScript
https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/typescriptsdk.htm
Other
68 stars 50 forks source link

Reading a JSON from object storage #276

Closed pedropmartiniano closed 4 months ago

pedropmartiniano commented 4 months ago

I'm having problems on transform the returned Stream from the GetObjectResponse to string and finally to a json, and I need help on how I can do that (I already tryed the examples from this repository), and I'm still having problems to get the json from my GetObjectResponse, here's my code:

            const response = await storageClient.getObject({
                bucketName: 'my-bucket',
                objectName: 'my-json-object-path',
                namespaceName: 'my-namespace'
            })

            async function streamToString(getObjectResponse: any) {
                const data = await consumers.buffer(getObjectResponse.value as NodeJS.ReadableStream).toString()
                return data
            }

            const supposedJson = await streamToString(response)

            // I'm not having my json here
            console.log(supposedJson)

May someone help me?

JoshuaWR commented 4 months ago

Hi @pedropmartiniano, is there any error output that you could share? From this, it's hard to tell where exactly the error is happening (is there an issue with the call to .getObject(), is streamToString() not working, etc.). Also, could you share which example you used? Looking at this example for object storage, we convert the getObject() response with getObjectResponse.value as st.Readable, where as you use getObjectResponse.value as NodeJS.ReadableStream. This may lead to slightly different behavior.

PedroMartiniano commented 4 months ago

(This is my personal account) The getObject() function is returning a object where It has a "value" atribute that has the following value:

ReadableStream { locked: false, state: 'readable', supportsBYOB: false}

But I'm having a problem to transform the stream in the json from the bucket, I already tried all the examples from the link you sended to me.

In this example, still with the asyncs and awaits(I already tried with then & catch too), the console is returning me the following state:

[Object promise]
JoshuaWR commented 4 months ago

I was able to get it to work with then(). In your case it would be supposedJson.then(function (result) { console.log((result as string)) }). This printed the json string as expected for me. When you tried the then keyword, did it look like this?

pedropmartiniano commented 4 months ago

I managed to make it work.

That's the solution that I found:

            const response = await storageClient.getObject({
                bucketName: 'my-bucket',
                objectName: `my-json-path`,
                namespaceName: 'my-namespace'
            })

            let passedValue = await new Response(response.value as BodyInit).text()
            let valueToJson: RootJsonPostoProps = JSON.parse(passedValue)