【问题标题】:create object with python ibm-cos-sdk get doesn't work使用 python ibm-cos-sdk get 创建对象不起作用
【发布时间】:2019-04-23 14:12:20
【问题描述】:

我需要帮助...我无法从我在 Watson Studio 中创建的笔记本访问我使用 IBM Cloud 仪表板创建的 COS 中的存储桶。调用这个bucket3。

最初,我使用 IBM Watson“添加资产”从单个 f2.zip (csv) 文件创建了一个 bucket2,并且我能够访问 f2.zip。调用这个bucket2。

f2.zip 上传到 bucket2 - 后来发现它是在以前的 COS 存储上创建的; IE。在 IBM Cloud“cloud-object-storage-nl”上创建的资产。调用这个bucket1。我在 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 Pythonibm-cos-sdk 的示例。

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

  1. 当我从 IBM Cloud 生成的存储桶凭证中使用 endpoint_url = 'endpoints' 时)- ibm_api_key_id 无关紧要。

成功:当我使用 endpoint_url = 'endpoint_url' 表单时,Watson Studio 生成的 watson 凭证...写入 bucket2 而不管 ibm_api_key_id(bucket2 或 bucket3)

代码:

# 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
                   )

【问题讨论】:

  • 发现我没有使用 bucket3 私有端点。更改了代码以包含 bucket2 和 bucket3 的私有端点。现在我的错误是:NoSuchKey:调用PutObject操作时发生错误(NoSuchKey):指定的键不存在。

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


【解决方案1】:

所需的解决方案:

  1. 导入具有bucketname的bucket.configuration.CRN
  2. 导入“Writer”ServiceCredential,然后
  3. 在调用 cos.Object() 时设置 bucketName 和相应的 kwargs。

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

更正了代码示例以反映更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2018-10-24
    • 2021-02-06
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多