Skip to main content
 首页 » 编程设计

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

2024年09月03日33zhenyulu

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

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

f2.zip 已上传到 bucket2 - 后来发现它是在以前的 COS 存储上创建的; IE。在 IBM Cloud“cloud-object-storage-nl”上创建的 Assets 。称这个 bucket1。我在 IBM Cloud 名称 = 'cloud-object-storage-xx' 上有一个 Lite COS。

我可以读取 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 凭据时...写入 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 
                   ) 

请您参考如下方法:

所需的解决方案:

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

关键是将存储桶 endpoint_url 设置为相应存储桶的私有(private)/公共(public)端点。

代码示例已更正以反射(reflect)更改。