IBM / ibm-cos-sdk-js

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

Using Service Credential JSON results in JS error #72

Closed necccc closed 3 years ago

necccc commented 3 years ago

Following the instructions in the docs, I've created a ~/.bluemix/cos_credentials file, and saved my service credential there from what the COS generated:

{
  "apikey": "REDACTED",
  "endpoints": "https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints",
  "iam_apikey_description": "Auto-generated for key REDACTED,
  "iam_apikey_name": "evidence-locker",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
  "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/REDACTED::serviceid:ServiceId-REDACTED",
  "resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:a/REDACTED:REDACTED::"
}

If I try to run a simple query, it results in a JS error

var bucketName = "my-bucket-name";
var cos = new ibm.S3({});

cos.listObjectsV2({
    Bucket: bucketName
}, (...args) => {
    console.log(args)
})
/Users/nec/ibm/cos-test/node_modules/ibm-cos-sdk/lib/config.js:388
    if (this.credentials.accessKeyId || this.credentials.tokenManager) {
                         ^

TypeError: Cannot read property 'accessKeyId' of null
    at Config.update (/Users/nec/ibm/cos-test/node_modules/ibm-cos-sdk/lib/config.js:388:26)
    at features.constructor.loadServiceClass (/Users/nec/ibm/cos-test/node_modules/ibm-cos-sdk/lib/service.js:78:14)
    at features.constructor.Service [as constructor] (/Users/nec/ibm/cos-test/node_modules/ibm-cos-sdk/lib/service.js:27:29)
    at new features.constructor (/Users/nec/ibm/cos-test/node_modules/ibm-cos-sdk/lib/util.js:624:24)
    at Object.<anonymous> (/Users/nec/ibm/cos-test/index.js:37:11)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
mriedem commented 3 years ago

I was hitting something similar (using version 1.7.0). I'm not using ~/.bluemix/cos_credentials like the docs say, I'm instead loading credentials and configuration via a config.js file in my app and the values come from environment variables. I was getting the same error until I actually had the credentials set in the environment.

Reading the docs on https://ibm.github.io/ibm-cos-sdk-js/AWS/Credentials.html it says:

Note that this class typically does not need to be constructed manually, as the AWS.Config and AWS.Service classes both accept simple options hashes with the three keys. These structures will be converted into Credentials objects automatically.

So I guess part of the bug is it fails to find the credentials so the Credentials object doesn't get created and then eventually there is a null TypeError. Why it's not finding ~/.bluemix/cos_credentials like the docs say to use, I don't know, but if it doesn't find credentials it should also avoid a TypeError and give a better error message.

sandersrIBM commented 3 years ago

We are looking into this issue. It is being tracked as a defect internally.

huineng commented 3 years ago

I have the same problem, but it runs on my local , but not in Openshift on cloud (same issue as above). it's not even json, it's a standard js object

huineng commented 3 years ago

ps if it might help :: i changed accessKeyId to apiKeyId and it worked just those 3 i needed

apiKeyId endpoint serviceInstanceId

sandersrIBM commented 3 years ago

Since we are tracking this issue internally, we plan to close this issue in one week.