在 python 3.6.2 上使用 ibm-cos-sdk 时,我得到一个 Unexpected keyword argument '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))
但是在运行时,我得到以下输出:
回溯(最后一次调用):文件“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 行,在 import botocore.session 文件中“/home/giovanni/Downloads/boto-test/lib/python3.5/site-packages/botocore/session.py”,第 37 行,在 import botocore.credentials 文件中“/home/giovanni/Downloads/boto-test/ lib/python3.5/site-packages/botocore/credentials.py",第 27 行,在 import httplib ImportError: No module named 'httplib'
我在哪里做错了什么?我应该在我的 virtualenv 中安装 botocore 吗?