-1

Iam trying to upload an image to object storage container and get the url of that image deployed on bluemix using a node js app.To achieve this i need to use a post or put api call.I could able to authenticate with the object storage but not able to achieve the functionality through the api calls.So,I need some help on the api calls.So can some one help me out in this if you had worked on such kind of api calls with images on object storage.(using object-storage npm).Even share any kind of sample working api calls.Any help appreciated.

H Varma
  • 440
  • 1
  • 10
  • 25

1 Answers1

2

Object storage api's are derived from the OpenStack Swift API spec. In order to add an object of any sort to a Bluemix Object Storage container, you'll need to do 2 things:

  1. Authenticate to the Object Storage instance to obtain an authorization token.
  2. Perform actions on the container using the token obtained.

I assume that you already have access to the JSON credentials provided by the object storage service ... similar to:

{

 "auth_url": "https://identity.open.softlayer.com",

 "domainId": "nice_long_hex_value",

 "domainName": "some_number",

 "password": "not_gonna_tell_you",

 "project": "object_storage_hex_value",

 "projectId": "project_hex_value",

 "region": "dallas",

 "userId": "another_fine_hex_value",

 "username": "some_text_with_hex_values"
}

Step 1: Obtain X-Auth-token. 4 items (user_id, user_name, password and auth_url) should come from your provided credentials.

curl -i -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "another_fine_hex_value",
                    "password": "not_gonna_tell_you"
                }
            }
        },
        "scope": {
            "project": {
                "id": "project_hex_value"
            }
        }
    }
}' "{auth_url}/v3/auth/tokens" | tee response.txt | grep X-Subject-Token | sed 's/.*X-Subject-Token: \([^ ]*\).*/\1/g' | tee >(awk '{printf("\nX-Auth-Token: %s\n\nJSON Response Body:\n", $0)}' > /dev/tty) | sed -n '/{/,$p' <response.txt | python -m json.tool && rm response.txt

This should result in a 500+ Line JSON Response BODY (take note of the public interface for the region of dallas within the swift endpoints array) similar to …

{
  "token": {
    "methods": [
      "password"
    ],
    "roles": [
      {
        "id": "redacted",
        "name": "ObjectStorageOperator"
      }
    ],
    "expires_at": "2016-03-09T20:26:39.192753Z",
    "project": {
      "domain": {
        "id": "some_hex_value",
        "name": "some_int"
      },
      "id": "another_hex_value",
      "name": "one_more_hex_value"
    },
    "catalog": [
        ...
      {
        "endpoints": [
          {
            "region_id": "london",
            ...
          },
          {
            ...
          },
          {
            "region_id": "dallas",
            "url": "https://dal.objectstorage.open.softlayer.com/v1/AUTH_",
            "region": "dallas",
            "interface": "public",
            "id": "some_unique_id"
          },
          {
            ...
          },
          {
            ...
          },
          {
            ...
          }
        ],
        "type": "object-store",
        "id": "hex_values_rock",
        "name": "swift"
      },
      ...
    ],
    "extras": {},
    "user": {
      "domain": {
        "id": "hex_value",
        "name": "another_fine_int"
      },
      "id": "tired_of_hex_values_yet?",
      "name": "cheers_one_more_hex_value_for_the_road"
    },
    ...
  }
}

Specifically, we want to identify the Swift Object Storage API url in the form: https://dal.objectstorage.open.softlayer.com/v1/AUTH_some-hex-value https://dal.objectstorage.open.softlayer.com/v1/AUTH_some-hex-value is linked to your desired object storage region (dallas, london, …) and associated with a public interface. This will be found within the endpoints section which includes the name “swift”.

Even more importantly, within the generated HTTP Response Header of this /v3/auth/tokens call is an authentication token that we also need to record to facilitate subsequent authenticated HTTP API calls.

Here is a sample of the HTTP Response Headers

Connection: Keep-Alive
Content-Length: 12089
Content-Type: application/json
Date: Wed, 09 Mar 2016 19:26:39 GMT
Keep-Alive: timeout=5, max=21
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_wsgi/3.4 Python/2.7.5
Vary: X-Auth-Token
X-Subject-Token: gAAAAABW4Hjv5O8yQRwYbkV81s7KC0mTxlh_tXTFtzDEf3ejsP_CByfvvupOeVWWcWrB6pfVbUyG5THZ6qM1-BiQcBUo1WJOHWDzMMrEB5nru69XBd-J5f5GISOGFjIxPPnNmEDZT_pahnBwaBQiJ8vrg9p5obdtRJeuxk7ADVRQFcBcRhAL-PI
x-openstack-request-id: req-26a078fe-d0a7-4a75-b32d-89d3461c55f1

The X-Subject-Token is the important response header. Its value will be reused within all subsequent HTTP Request Headers using the header X-Auth-Token. Obvious, right?

Step 2: With this token, let's add an object to a container named "ibmjstart".

curl -s -X PUT -i -H "Content-Type: text/plain"\
    -H "X-Auth-Token: X-Subject-Token from above"\
    -H "Cache-Control: no-cache"\
  -d "Awesome sauce is best served warm" "{API AUTH URL obtained above}/ibmjstart/test.txt"

If all goes well, this should result in a new container named ibmjstart which contains a text file named test.txt with a single line of content.

Sanjay.Joshi
  • 294
  • 1
  • 12
  • I heard some of these APIs are getting deprecated. Are there any updates as to how the new approach would be for object actions? Thanks. – geofrey Jan 19 '18 at 18:39