Неожиданный аргумент ключевого слова 'ibm_api_key_id' при использовании ibm-cos-sdk?
При использовании ibm-cos-sd k на python 3.6.2 я получаю аргумент ошибки "Неожиданный аргумент ключевого слова" ibm_api_key_id "".
Я установил библиотеку в чистой виртуальной среде, используя следующие шаги:
virtualenv --python=python3.5 boto-test
source boto-test/bin/activate
pip install ibm-cos-sdk
Затем я попытался запустить пример отсюда:
import boto3
import json
import requests
import random
from botocore.client import Config
from pprint import pprint
with open('./credentials.json') as data_file:
credentials = json.load(data_file)
print("Service credential:")
print(json.dumps(credentials, indent=2))
print("")
print("Connecting to COS...")
# Rquest detailed enpoint list
endpoints = requests.get(credentials.get('endpoints')).json()
#import pdb; pdb.set_trace()
# Obtain iam and cos host from the the detailed endpoints
iam_host = (endpoints['identity-endpoints']['iam-token'])
cos_host = (endpoints['service-endpoints']['cross-region']['us']['public']['us-geo'])
api_key = credentials.get('apikey')
service_instance_id = credentials.get('resource_instance_id')
# Constrict auth and cos endpoint
auth_endpoint = "https://" + iam_host + "/oidc/token"
service_endpoint = "https://" + cos_host
print("Creating client...")
# Get bucket list
cos = boto3.client('s3',
ibm_api_key_id=api_key,
ibm_service_instance_id=service_instance_id,
ibm_auth_endpoint=auth_endpoint,
config=Config(signature_version='oauth'),
endpoint_url=service_endpoint)
# Call S3 to list current buckets
response = cos.list_buckets()
# Get a list of all bucket names from the response
buckets = [bucket['Name'] for bucket in response['Buckets']]
# Print out the bucket list
print("Current Bucket List:")
print(json.dumps(buckets, indent=2))
print("---")
result = [bucket for bucket in buckets if 'cos-bucket-sample-' in bucket]
print("Creating a new bucket and uploading an object...")
if len(result) == 0 :
bucket_name = 'cos-bucket-sample-' + str(random.randint(100,99999999));
# Create a bucket
cos.create_bucket(Bucket=bucket_name)
# Upload a file
cos.upload_file('./example.py', bucket_name, 'example-object')
# Call S3 to list current buckets
response = cos.list_buckets()
# Get a list of all bucket names from the response
buckets = [bucket['Name'] for bucket in response['Buckets']]
# Print out the bucket list
print("New Bucket List:")
print(json.dumps(buckets, indent=2))
print("---")
else :
bucket_name = result[0];
# Call S3 to list current objects
response = cos.list_objects(Bucket=bucket_name)
# Get a list of all object names from the response
objects = [object['Key'] for object in response['Contents']]
# Print out the object list
print("Objects in %s:" % bucket_name)
print(json.dumps(objects, indent=2))
Но при запуске я получаю следующий вывод:
Traceback (последний вызов был последним): файл "boto3test.py", строка 1, в файле импорта boto3 "/home/giovanni/Downloads/boto-test/lib/python3.5/site-packages/boto3/init.py", строка 16, из файла boto3.session для импорта файла сессии "/home/giovanni/Downloads/boto-test/lib/python3.5/site-packages/boto3/session.py", строка 27, в файле импорта botocore.session "/home/giovanni/Downloads/boto-test/lib/python3.5/site-packages/botocore/session.py", строка 37, в файле импорта botocore.credentials "/home/giovanni/Downloads/boto-test/lib/python3.5/site-packages/botocore/credentials.py", строка 27, в import httplib ImportError: Нет модуля с именем 'httplib'
Где я делаю что-то не так? Должен ли я установить Botocore в моем virtualenv?
2 ответа
Отредактируйте botocore/credentials.py и измените импорт httplib для импорта http.client как httplib
Я обнаружил проблему: библиотека IBM https://github.com/IBM/ibm-cos-sdk-python-core, является их собственной версией библиотеки botocore, однако на credentials.py из их репозитория есть ссылка на библиотеку, которая была переименована в Python 3 (httplib -> http.client).
Поэтому я решил заменить строку 27 файла credentials.py в моем локальном каталоге установки с:
import httplib
Для того, чтобы:
import http.client as httplib
По делу был открытый вопрос (#1), однако я не видел, потому что репозитории не были связаны друг с другом, и я все еще изучаю, как работают библиотеки IBM.