api调用将图像上传到bluemix节点js app上的对象存储容器

时间:2016-08-28 18:00:24

标签: node.js npm ibm-cloud object-storage

Iam尝试将图像上传到对象存储容器,并使用节点js app获取部署在bluemix上的该图像的url。要实现这一点,我需要使用帖子或放置api调用。我可以使用对象存储,但无法通过api调用实现功能。所以,我需要一些关于api调用的帮助。如果你曾经使用对象存储上的图像处理这种类型的api调用,可以帮助我。 (使用对象存储npm)。甚至共享任何类型的工作api调用示例。非常感谢帮助。

1 个答案:

答案 0 :(得分:2)

对象存储API来自OpenStack Swift API规范。为了将任何类型的对象添加到Bluemix对​​象存储容器,您需要做两件事:

  1. 对Object Storage实例进行身份验证以获取授权令牌。
  2. 使用获得的令牌对容器执行操作。
  3. 我假设您已经可以访问对象存储服务提供的JSON凭证...类似于:

    {
    
     "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"
    }
    

    第1步:获取X-Auth-token。 4项(user_id,user_name,password和auth_url)应来自您提供的凭据。

    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
    

    这应该会产生一个500+ Line JSON Response BODY(注意swift端点数组中dallas区域的公共接口),类似于......

    {
      "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"
        },
        ...
      }
    }
    

    具体来说,我们希望以下列形式识别Swift Object Storage API网址: https://dal.objectstorage.open.softlayer.com/v1/AUTH_some-hex-value https://dal.objectstorage.open.softlayer.com/v1/AUTH_some-hex-value是 链接到您想要的对象存储区域(dallas,london,...)并与公共接口相关联。这将在端点部分中找到,其中包含名称“swift”。

    更重要的是,在生成的HTTP响应标头中,此/ v3 / auth / tokens调用是一个身份验证令牌,我们还需要记录这些令牌以便于后续经过身份验证的HTTP API调用。

    以下是HTTP响应标头的示例

    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
    

    X-Subject-Token是重要的响应头。它的值将在所有后续HTTP请求标头中使用标头X-Auth-Token重用。很明显,对吧?

    第2步:使用此令牌,我们将一个对象添加到名为“ 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"
    

    如果一切顺利,这应该会产生一个名为ibmjstart的新容器,其中包含一个名为 test.txt 的文本文件,其中包含一行内容。