oracle/oci-typescript-sdk

Reading a JSON from object storage

Closed this issue · 4 comments

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?

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.

(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]

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?

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)