该操作未在python文件中产生有效的JSON对象

时间:2019-10-08 19:17:13

标签: python json ibm-cloud

我正在使用IBM云功能的两个操作-write1write2(均使用PYTHON)。

我创建了一个序列,该序列应将值从write1传递到write2

我在write1动作中编写了PYTHON代码,但抛出了一些 JSON错误

写入1个Python文件:*

    import os
    import sys
    import json
    import requests
    import ibm_boto3
    from ibm_botocore.client import Config, ClientError

    cos = ibm_boto3.resource("s3",
          ibm_api_key_id='my-api-key',
          ibm_service_instance_id='my-instance-id',
          config=Config(signature_version="oauth"),
         endpoint_url='https://s3.eu-gb.cloud-object-storage.appdomain.cloud'
         )

    def get_item(bucket_name, item_name):
      a={"Retrieving item from bucket":bucket_name , "key": item_name}
      print(json.dumps(a))
    try:
        file = cos.Object(bucket_name, item_name).get()
        return file["Body"].read()
    except ClientError as be:
        w={"CLIENT ERROR":be}
        print(json.dumps(w))
    except Exception as e:
        y={"Unable to retrieve file contents":e}
        print(json.dumps(y))


    def test():
      x = get_item('cloud-college-bucket0','abc.txt') 
      print(x.decode('utf-8'))

    if x is not None:
        string_in_uppercase = x.upper();
        n={"String in Uppercase =":string_in_uppercase.decode('utf-8')}
        b=json.dumps(n)
        print(b)

    def main(dict):
      return test()      

    if __name__ == '__main__':
      main()

引发的错误:

结果:

{
  "error": "**The action did not produce a valid JSON response**: null\n"
}

Logs:

[
  "2019-10-08T13:01:56.339677Z    stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the post() function from 'ibm_botocore.vendored.requests'.  This is not a public API in ibm_botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.post() function instead.",

  "2019-10-08T13:01:56.339748Z    stderr: DeprecationWarning",

  "2019-10-08T13:01:56.339755Z    stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/models.py:169: DeprecationWarning: 

Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working",

  "2019-10-08T13:01:56.339759Z    stderr: if isinstance(hook, collections.Callable):",

  "2019-10-08T13:01:56.339678Z    stdout: {\"Retrieving item from bucket\": \"cloud-college-bucket0\", \"key\": \"abc.txt\"}",

  "2019-10-08T13:01:56.339772Z    stdout: hello friends",

  "2019-10-08T13:01:56.339776Z    stdout: {\"String in Uppercase =\": \"HELLO FRIENDS\"}",

  "2019-10-08T13:01:56.340Z       stderr: The action did not initialize or run as expected. Log data might be missing."

]

它说import request是我做的,但问题仍然存在。

我也说过使用request.post函数,但是如何使用和在哪里使用是我无法理解的。 以及如何解决此JSON问题?

所需的输出显示在日志中。

1 个答案:

答案 0 :(得分:0)

从一开始,我就可以看到test函数仅显示JSON,但从不返回。但是,如果您看到示例Python动作,则它始终应返回JSON

import sys

def main(dict):
    return { 'message': 'Hello world' }

此外,您可以在执行操作之前使用Python运行时here查看受支持的软件包列表。

如果您的软件包不在列表中,则可以始终package Python code with a virtual environment in .zip filesPackaging code in Docker images