在 Watson 中,资产文件保存在 Cloud Object Store 或 COS 中。您必须将图像从 COS 下载到笔记本服务器文件系统,然后您可以将笔记本中的文件作为常规本地文件引用。
我使用 cos api 来获取文件。https://github.com/IBM/ibm-cos-sdk-python。
https://console.bluemix.net/docs/services/cloud-object-storage/libraries/python.html#using-python
首先,找出您的凭据是什么
- 突出显示笔记本单元格,
- 点击数据菜单,
- 选择文件,
- “插入代码”
- 凭据。
然后,您可以使用 API 将文件下载到本地磁盘存储。例如,从 COS 下载文件:
# The following code contains the credentials for a file in your IBM Cloud Object Storage.
# You might want to remove those credentials before you share your notebook.
credentials_1 = {
'IBM_API_KEY_ID': '**************************************',
'IAM_SERVICE_ID': 'iam-ServiceId-**************************',
'ENDPOINT': 'https://s3-api.us-geo.objectstorage.service.networklayer.com',
'IBM_AUTH_ENDPOINT': 'https://iam.ng.bluemix.net/oidc/token',
'BUCKET': '********************************',
'FILE': 'file.xlsx'
}
from ibm_botocore.client import Config
import ibm_boto3
def download_file_cos(credentials, local_file_name, key):
cos = ibm_boto3.client(service_name='s3',
ibm_api_key_id=credentials['IBM_API_KEY_ID'],
ibm_service_instance_id=credentials['IAM_SERVICE_ID'],
ibm_auth_endpoint=credentials['IBM_AUTH_ENDPOINT'],
config=Config(signature_version='oauth'),
endpoint_url=credentials['ENDPOINT'])
try:
res=cos.download_file(Bucket=credentials['BUCKET'], Key=key, Filename=local_file_name)
except Exception as e:
print(Exception, e)
else:
print("Dowloaded:", key, 'from IBM COS to local:', local_file_name)
在笔记本单元格列表目录内容中:
%%script bash
ls -l
# to list all .png files in COS you can use a function like this:
def list_objects(credentials):
cos = ibm_boto3.client(service_name='s3',
ibm_api_key_id=credentials['IBM_API_KEY_ID'],
ibm_service_instance_id=credentials['IAM_SERVICE_ID'],
ibm_auth_endpoint=credentials['IBM_AUTH_ENDPOINT'],
config=Config(signature_version='oauth'),
endpoint_url=credentials['ENDPOINT'])
return cos.list_objects(Bucket=credentials['BUCKET'])
response = list_objects(credentials_1)
for c in response['Contents']:
if c['Key'].endswith('.png'):
print(c['Key'], "last modified:", c['LastModified'])