使用python ibm-cos-sdk创建对象,get不起作用

时间:2018-11-21 03:13:37

标签: python ibm-cloud cos watson-studio ibm-cloud-storage

我需要帮助...我无法从在Watson Studio中创建的笔记本中访问使用IBM Cloud仪表板创建的COS存储桶。将此存储桶称为3。

最初,我使用IBM Watson'Add Asset'从单个f2.zip(csv)文件创建了bucket2,并且能够访问f2.zip。将此存储桶称为2。

f2.zip已上传到bucket2-后来发现它是在以前的COS存储器上创建的;即。在IBM Cloud'cloud-object-storage-nl'上创建的资产。将此存储桶称为1。我在IBM Cloud上只有一个Lite COS,名称为“ cloud-object-storage-xx”。

我可以阅读f2.zip,并且可以使用“文件UI”按钮生成的凭据(cred_b2_editor)在bucket2中创建新的f2.zip。

IBM Cloud仪表板显示:

bucket1 us-geo Standard
bucket2 us-geo Standard
bucket3 us-east Standard

我正在使用Creating a new text file in Using Python ibm-cos-sdk中的示例。

在2种情况下,出现“ ClientError:调用PutObject操作:请求实体太大”时发生错误(413),失败:

  1. 当我使用endpoint_url ='endpoints'形式由IBM Cloud生成的存储桶凭证时)-ibm_api_key_id没关系。

成功:当我使用endpoint_url ='endpoint_url'形成由Watson Studio生成的watson凭据时...不管ibm_api_key_id(bucket2或bucket3)如何写入bucket2

代码:

# Point to generated credentials
credDict = dict(b2 = cred_b2_editor,
                b3 = cred_b3_writer,
                watson = cred_watson
                )


bucketName = 'b3'
kwargs = dict(
    ibm_api_key_id=credDict[bucketName]['ibm_api_key_id'],
    ibm_service_instance_id=credDict[bucketName]['cred']['iam_serviceid_crn'], #COS_RESOURCE_CRN,
    ibm_auth_endpoint=COS_AUTH_ENDPOINT,
    config=Config(signature_version="oauth"),
    endpoint_url=credDict[bucketName]['ep_private']
    )
buckName = bucketDict[bucketName].split(':')[-1:][0]

print(buckName, kwargs['ibm_api_key_id'], kwargs['endpoint_url'])
cos = ibm_boto3.resource("s3", **kwargs)

#---> fix: bucketname needed to change with each bucket...
#---> fix: endpoint_url needs to point to private/public endpoint 
cos.Object(buckName, csvBN.replace('.csv','.zip')).put(
                Body=zbuf
            )

凭据代码-以下所有内容均已生成

'''
Cloud Resource Name or 'bucket ID string'

The last field is the `bucketName`
'''
bucketDict = dict(b2 = 'crn:v1:bluemix:public:cloud-object-storage:global:a/<IDNum>:<serviceID-seperated>:bucket:bucket2',
               b3 = 'crn:v1:bluemix:public:cloud-object-storage:global:a/<IDNum>:<serviceID-seperated>:bucket:bucket3'
              )

# Bucket2 Editor credentials - created by IBM Watson automatically
cred_b2_editor = {
  "apikey": "....",
  "cos_hmac_keys": {
    "access_key_id": "...",
    "secret_access_key": "..."
  },
  "endpoints": "https://cos-service.bluemix.net/endpoints",
  "iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:cloud-object-storage:global:a/<ServiceIDNum>:<COS_ID>::",
  "iam_apikey_name": "auto-generated-apikey-<apikey_2>",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
  "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/<ServiceIDNum>::serviceid:ServiceId-<serviceID_2>",
  "resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:a/<ServiceIDNum>:<COS_ID>::"
}

## Bucket3 Create via IBM Cloud "New Credentials"
cred_b3_writer = {
  "apikey": "4hEJq-slh28Atvq3XnekZ4YOl0yWiv4LbFigoPS3oiuL",
  "endpoints": "https://cos-service.bluemix.net/endpoints",
  "iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:cloud-object-storage:global:a/<ServiceIDNum>-<COS_ID>::",
  "iam_apikey_name": "auto-generated-apikey-<apikey_3>",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
  "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/<ServiceIDNum>::serviceid:ServiceId-<ServiceID_3>",
  "resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:a/<ServiceIDNum>:<COS_ID>::"
}

# Created inside juptyer notebook 10/01 button
cred_b2_cos = dict(ibm_api_key_id=cred_b2['apikey'],
                   ibm_auth_endpoint="https://iam.ng.bluemix.net/oidc/token",
                   config=Config(signature_version='oauth'),
                   ep_private='https://s3-api.us-geo.objectstorage.service.networklayer.com',
                   ep_public = 'https://s3-api.us-geo.objectstorage.softlayer.net',
                   cred = cred_b2_editor
                   )
cred_b3_cos = dict(ibm_api_key_id=cred_b3['apikey'],
                   ibm_auth_endpoint="https://iam.ng.bluemix.net/oidc/token",
                   config=Config(signature_version='oauth'),
                   ep_private = 'https://s3.us-east.objectstorage.service.networklayer.com',
                        ep_public = 'https://s3.us-east.objectstorage.softlayer.net',
                   cred = cred_b3_writer
                   )

1 个答案:

答案 0 :(得分:2)

所需的解决方案:

  1. 导入具有存储桶名称的bucket.configuration.CRN
  2. 导入“作家” ServiceCredential,然后
  3. 在调用cos.Object()时设置bucketName和相应的kwarg。

密钥是将存储桶endpoint_url设置为相应存储桶的私有/公共端点。

代码示例已得到纠正,以反映更改。