Couchbase API存储桶创建

时间:2017-08-14 09:56:49

标签: python couchbase

我尝试使用带有python(3.5)的请求模块创建Couchbase(v.4.6.2)存储桶,但没有运气。这是代码:

import requests
import json
url = 'http://127.0.0.1:8091/pools/default/buckets'
cdb_login = 'Administrator'
cdb_pass = 'adminpassw0rd'
payload = {'bucketType': 'couchbase',
           'authType': 'sasl',
           'saslPassword': 'passw0rd',
           'name': 'newBucket01',
           'ramQuotaMB': 512,
           'replicaNumber': 1,
           'conflictResolutionType':'lww',
           'flushEnabled': 1}

r = requests.post(url, auth=(cdb_login, cdb_pass), data=json.dumps(payload))
print(r.status_code, r.reason, r.text)

回复是错误的:

400 Bad Request {"errors":{"authType":"invalid authType"},"summaries":{"ramSummary":{"total":4831838208,"otherBuckets":641728512,"nodesCount":1,"perNodeMegs":0,"thisAlloc":0,"thisUsed":0,"free":4190109696},"hddSummary":{"total":317653053440,"otherData":123855113749,"otherBuckets":29577092,"thisUsed":0,"free":193768362599}}}

我试图强制使用HTTPDigestAuth而不是像这样的基本默认值:

r = requests.post(url, auth=requests.auth.HTTPDigestAuth(cdb_login, cdb_pass), data=json.dumps(payload))

它抛出

401 Unauthorized 

(请注意,登录名和密码与上述相同)。所以它也不起作用。

'管理员'和' adminpassw0rd'是我在127.0.0.1:8091访问Couchbase管理GUI时使用的日志记录和密码。如果我使用来自bash的CURL,这些参数可以工作并创建存储桶:

import subprocess
create_bucket_bash = ['curl', '-X', 'POST', '-u', 'Administrator:adminpassw0rd', 'http://127.0.0.1:8091/pools/default/buckets',
                      '-d', 'name=newBucket01',
                      '-d', 'conflictResolutionType=lww',
                      '-d', 'authType=sasl',
                      '-d', 'ramQuotaMB=512',
                      '-d', 'saslPassword=passw0rd',
                      '-d', 'bucketType=couchbase']
subprocess.call(create_bucket_bash)

请求错过了什么或做错了什么?

1 个答案:

答案 0 :(得分:1)

REST端点期望URL编码的值不是JSON,如果您只是直接传递有效负载它应该工作。

r = requests.post(url, auth=(cdb_login, cdb_pass), data=payload)

您是否尝试过使用Python SDK的群集管理API?它可以以交互方式为您创建存储桶。

例如:

from couchbase.admin import Admin

admin = Admin('Administrator', 'password', host='10.112.162.101', port=8091)

# create bucket
admin.bucket_create('bucket_name',
                    bucket_type='couchbase',
                    bucket_password='passw0rd',
                    replicas=1,
                    ram_quota=100,
                    flush_enabled=False)

# optionally wait for bucket to be ready
admin.wait_ready('bucket_name',
                 timeout=10.0,
                 sleep_interval=0.2)