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
71 stars 51 forks source link

TypeError: imageStream.pipe is not a function #223

Open shamnad-sherief opened 1 year ago

shamnad-sherief commented 1 year ago

I'm encountering an issue while using the GetObjectResponse object from the OCI Object Storage library. When trying to use the .pipe() method on the value property of the GetObjectResponse, I receive the following error:

TypeError: imageStream.pipe is not a function

Expected Behavior: The .pipe() method should work as expected on the value property, allowing the stream content to be piped to a response object.

Actual Behavior: Encountering the "TypeError: imageStreamValue.pipe is not a function" error when attempting to use .pipe() on the value property.

#oci-object-storage-service.ts

async getImageAsStream(folderName: string, imageName: string): Promise<GetObjectResponse | null> {
    try {
      const authenticationProvider =  this.createAuthenticationProvider();
      const objectStorageClient = new ObjectStorageClient({
        authenticationDetailsProvider: authenticationProvider,
      });

      const objectName = folderName + '/' + imageName;

      const getObjectRequest = {
        namespaceName: process.env.OCI_NAMESPACE + '',
        bucketName: process.env.OCI_BUCKET_NAME + '',
        objectName: objectName,
      };

      const getObjectResponse = await objectStorageClient.getObject(getObjectRequest);

      if (getObjectResponse) {

        return getObjectResponse;
      }

      return null;
    } catch (error) {
      console.error('Error fetching image stream:', error);
      return null;
    }
  }

Below is my controller:-

#image.controller.ts 

@get('/image-stream/{folderName}/{imageName}')
  async getImageStream(
    @param.path.string('folderName') folderName: string,
    @param.path.string('imageName') imageName: string,
    @inject(RestBindings.Http.RESPONSE) response: Response,
  ): Promise<any> {
    const ociResponse = await this.cloudStorageService.getImageAsStream(folderName, imageName);

    if (ociResponse) {

        imageStream = ociResponse.value;
        response.setHeader('Content-Type', 'image/png');

        imageStream.pipe(response);

    } else {
      response.status(404).send('Image not found');
    }
  }
}
KartikShrikantHegde commented 1 year ago

Hi @shamnad-sherief , what version of the SDK and NodeJS are you using?

shamnad-sherief commented 1 year ago

Hi @shamnad-sherief , what version of the SDK and NodeJS are you using?

 "oci-sdk": "^2.68.0",
 node -v  >>   v18.16.0
JJDSNT commented 11 months ago

Hi, does your response has value? const response = await this.objectStorageClient.getObject(getObjectRequest); i got an empty response.value

shamnad-sherief commented 11 months ago

@JJDSNT No problem with response value.

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

But it is quiet different from the old version.

greg-md commented 6 months ago

@shamnad-sherief I have the same issue. Did you find a solution?

Later add. In node 20 getObject returns a web stream, which needs to be converted to a node stream:

if (response.value instanceof Readable) {
    return response.value;
}
return Readable.fromWeb(response.value);