Jupyter notebook 在Watson Studio的Jupyter笔记本中,如何引用上载到;资产;?

Jupyter notebook 在Watson Studio的Jupyter笔记本中,如何引用上载到;资产;?,jupyter-notebook,watson-studio,Jupyter Notebook,Watson Studio,在Watson Studio中,我正在Jupyter笔记本中编写代码,以使用Watson视觉识别自定义模型。 它适用于外部图像。 我还没有能够参考我上传到我的项目资产上的图像。 资产的url将访问整个页面,而不仅仅是图像: 谢谢在Watson中,资产文件保存在云对象存储或COS中。您必须将图像从COS下载到笔记本服务器文件系统,然后才能将笔记本中的文件作为常规本地文件引用 我使用cos api来获取文件 首先,通过以下方式了解您的凭据: 突出显示笔记本电池 点击数据菜单 选择文件 “插入到代

在Watson Studio中,我正在Jupyter笔记本中编写代码,以使用Watson视觉识别自定义模型。 它适用于外部图像。 我还没有能够参考我上传到我的项目资产上的图像。 资产的url将访问整个页面,而不仅仅是图像:


谢谢

在Watson中,资产文件保存在云对象存储或COS中。您必须将图像从COS下载到笔记本服务器文件系统,然后才能将笔记本中的文件作为常规本地文件引用

我使用cos api来获取文件

首先,通过以下方式了解您的凭据:

  • 突出显示笔记本电池
  • 点击数据菜单
  • 选择文件
  • “插入到代码”
  • 凭证
  • 然后,您可以使用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'])