0
votes

I am able to read files in bucket in COS using IAM token but unable to do the same using API key. I have set the following key in my request header:

Authorization: {api key}

Should I set anything else? Note I am not using HMAC credentials. below code has two options one with bearer token and the other with api key created for that Cos instance specifically for the bucket.

example code:

var request = require('request');

//using bearer token

var options = {

uri: 'https://{endpoint}/{bucket name}',
headers: {
'Authorization': 'bearer {token string}',
}
};

//using api key

var options = {
uri: 'https://{endpoint}/{bucketname}',
headers: {
'Authorization': '{{api key string for cos service id}',
}
};

function callback(error, response, body) {
console.log(error)
console.log(response.statusCode)
if (!error && response.statusCode == 200) {
console.log(body)
}
}

 request(options, callback);
2
Are you encountering error messages? If so, please include them so that people can help. - William 'Bill' Wentworth
I am getting 403 forbidden as response code.. - user1456110
Can you provide a minimum complete and verifiable example: stackoverflow.com/help/mcve - Chris Snow

2 Answers

0
votes

Just use the ibm-cos-sdk npm library here https://www.npmjs.com/package/ibm-cos-sdk. You just have to do the following and you're on your way.

var AWS = require('ibm-cos-sdk');
var util = require('util');

var config = {
    endpoint: '<endpoint>',
    apiKeyId: '<api-key>',
    ibmAuthEndpoint: 'https://iam.ng.bluemix.net/oidc/token',
    serviceInstanceId: '<resource-instance-id>',
};

var cos = new AWS.S3(config);
0
votes

API key should not be sent in the Authorization header at all. Its a key for one time generation of the token. As A. J. Alger pointed out, I would suggest using some SDK (list provided below) to eliminate the redundant work. Regrading the error you are getting while using the Node JS sdk, please provide more details on it.