IBM / ibm-cos-sdk-js

ibm-cos-sdk-js
Apache License 2.0
38 stars 19 forks source link

Compile failed when using ibm-cos-sdk on Node 16 #94

Closed oetlinge closed 2 years ago

oetlinge commented 2 years ago

Trying to incorporate ibm-cos-sdk into our project's translation loader to fetch translation files from COS, but running into compilation issues.

My project environment:

After installing ibm-cos-sdk into the workspace via yarn, imported it into our translation loader typescript module with

import * as COS from 'ibm-cos-sdk';

When compiling the project, the following errors show up in the console (were not present before installing ibm-cos-sdk):

./node_modules/cipher-base/index.js:2:16-43 - Error: Module not found: Error: Can't resolve 'stream' in '<project-dir>\node
_modules\cipher-base'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
./node_modules/ibm-cos-sdk/lib/event_listeners.js:559:21-44 - Error: Module not found: Error: Can't resolve 'util' in '<project-dir>\node_modules\ibm-cos-sdk\lib'
Did you mean './util'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (<project-dir>, node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
        - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "util": false }

The code that actually uses the ibm-cos-sdk is a slight adaptation of your sample code for listing bucket contents:

const config = {
    endpoint: 's3.us-east.cloud-object-storage.appdomain.cloud',
    apiKeyId: null,
    serviceInstanceId:
      'crn:v1:bluemix:public:cloud-object-storage:global:a/b7f8bd49d65cebd142cea6c1832090ca:a6a08289-757d-4f2a-88a5-7581ec12adba::',
    signatureVersion: null
  };
  const cosClient = new COS.S3(config);
  // Filter results to only include distinct parent folder of each *.json file
  const modules = await cosClient
    .listObjects({ Bucket: 'oetlinge-translation-poc', Prefix: 'TenantA' })
    .promise()
    .then((data) => {
      if (data?.Contents) {
        const keys = data.Contents.map((item) => {
          const keySegments = item.Key.split('/', 2);
          return keySegments?.[1];
        });
        return keys;
      }
    });

My endpoint has public read access without need for any authentication, so I wanted to test without values provided for apiKeyId and signatureVersion if possible.

IBMalok commented 2 years ago

FYI, we are looking into this and will get back to you.

IBMalok commented 2 years ago

@oetlinge - The team has been talking internally and the COS-SDK does not need to provide a fix. Could you close the ticket?

oetlinge commented 2 years ago

We've decided to issue simple http request to our COS bucket instead and not bother with ibm-cos-sdk.