For cURL
curl -X "POST" "https://iam.cloud.ibm.com/identity/token" \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "apikey={api-key}" \
--data-urlencode "response_type=cloud_iam" \
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey"
For Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
import json
#Get a token
headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
'apikey': os.environ['IBM_APIKEY'],
'response_type': 'cloud_iam',
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey'
}
response = requests.post('https://iam.cloud.ibm.com/identity/token', headers=headers, data=data)
print(response)
output = response.json()
#print(json.dumps(output, indent=4))
ibm_access_token = output['access_token']
print(ibm_access_token)
Python execution result
STATUS :<Response [200]>
{
"access_token":"eyJraWQ******************************************2Erh-Te-w",
"expires_in":3600,
}
#Very long tokens are valid for 1 hour (3600)=60 seconds x 60 minutes)
For cURL
curl "https://(endpoint)/" \
-H "Authorization: bearer (token)" \
-H "ibm-service-instance-id: (resource-instance-id)"
For Python
#Write the same python script until you get the token
# “#!/usr/bin/env from python to ibm_access_token = output['access_token']Until"
#I will omit it here.
#List of objects
headers = {
'Authorization': 'bearer ' + ibm_access_token
}
response = requests.get('https://s3.jp-tok.cloud-object-storage.appdomain.cloud/robocamera', headers=headers)
print("STATUS :" + str(response))
print("HEADERS:" + str(response.headers))
print("TEXT :" + str(response.text))
Python execution result
STATUS :<Response [200]>
HEADERS:{'Content-Length': '1938', 'ibm-sse-kp-enabled':<Omitted>'Content-Type': 'application/xml'}
TEXT :<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Omitted>
<Name>robocamera</Name>
<Contents>
<Key>ciel_face_s.png</Key><LastModified>2019-11-30T02:50:53.410Z</LastModified>
<Key>ghostpicture.jpg</Key><LastModified>2019-11-30T01:46:52.413Z</LastModified>
<Key>index.html</Key><LastModified>2019-11-24T14:48:55.793Z</LastModified>
For cURL
curl -X "PUT" "https://(endpoint)/(bucket-name)/(object-key)" \
-H "Authorization: bearer (token)" \
-H "Content-Type: (content-type)" \
-d "(object-contents)"
For Python
#Upload the camera image file to IBM Cloud Object Storage.
#In this example[/robocamera]Is the bucket name,[ghostpicture.jpg]Is the key name of the object.
headers = {
'Authorization': 'bearer ' + ibm_access_token,
'Content-Type': 'image/jpeg',
}
f = open('capture_output.jpg', 'rb')
img_data = f.read()
f.close()
response = requests.put(service_endpoint + '/robocamera/ghostpicture.jpg', headers=headers, data=img_data)
#When you want to check when debugging token acquisition
print("UPLOAD STATUS:" + str(response))
This is convenient.
Convert curl syntax to Python https://curl.trillworks.com/