简体   繁体   English

使用 Python 访问 aws ecr 时的参数错误

[英]argument errors when using using Python to access to aws ecr

here is the function:这是功能:

def sh(*command, read_output=False, **kwargs):
    command_text = " ".join(command)
    print(f"\t> {command_text}")
    try:
        if read_output:
            return check_output(command, **kwargs).decode("utf8")
        else:
            check_call(command, **kwargs)
    except CalledProcessError as failure:
        print(
            f'ERROR: "{command_text}" command reported failure! Return code {failure.returncode}.'
        )
        sys.exit(failure.returncode)

I'm trying to use this function to get aws erc get-login first, then use that returned login command to login to aws erc.我正在尝试使用此函数先获取 aws erc get-login,然后使用返回的登录命令登录到 aws erc。 here is my codes:这是我的代码:

result = sh('aws', 'ecr', 'get-login', '--no-include-email', read_output=True)
re = result.split()
sh(re)

then I get error:然后我得到错误:

command_text = " ".join(command)
TypeError: sequence item 0: expected str instance, list found

I think the sh function expect arguments something like `('docker', 'login', '-u', 'AWS', '-p'...), but how can I achieve this?我认为sh函数需要像`('docker', 'login', '-u', 'AWS', '-p'...) 这样的参数,但是我该如何实现呢?

You can use * to unpack list/tuple and function get it as many arguments您可以使用*来解压列表/元组,并通过函数获取尽可能多的参数

sh( *re )

Or you can remove * from *command in definiton或者您可以在定义中从*command中删除*

def sh(command, ...)

and then you can send it only as list/tuple然后您只能将其作为列表/元组发送

sh( re )

but you can also check if command is list or string但您也可以检查commandlist还是string

if isinstance(command, str): 
    command_text = command 
elif isinstance(command, list, tuple): 
    command_text = " ".join(command)

so then you can send it directly as one string.所以你可以直接将它作为一个字符串发送。

sh( 'aws ecr get-login --no-include-email' )

or list with strings或带字符串的列表

sh( ['aws', 'ecr', 'get-login', '--no-include-email'] )

BTW: Similar way works ** with dictionary and named arguments顺便说一句:类似的方式适用于字典和命名参数**

def fun(a=0, b=0, c=0):
    print('a:', a)
    print('b:', b)
    print('c:', c)

data = {'b':2}

fun(**data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python Docker API从AWS ECR获取Image哈希 - Using the Python Docker API to get an Image hash from an AWS ECR 使用 Python Lambda 函数的 AWS ECR 图像标签 - AWS ECR Image Tags using Python Lambda Function 使用Postgres时Django在AWS Beanstalk上出现错误 - Django on AWS Beanstalk errors when using Postgres 在for中使用时,“ int”类型的python参数不可迭代 - python argument of type 'int' is not iterable when using for in 从Windows 7使用时出现与AWS连接时出错 - Getting errors in connecting with AWS when using from windows 7 使用API​​网关尝试AWS Python Lambda函数中的访问参数时出错 - Error when trying access parameters in AWS Python Lambda function using API Gateway 使用 Python Argparse 时在可选参数中使用“-”的问题 - Problem using '-' inside an optional argument when using Python Argparse 使用 Selenium Python 访问 Facebook 元素但出现错误的简单程序 - Simple program to access Facebook elements using Selenium Python but getting errors 尝试使用pyspark访问greenplum表时发生错误 - Errors when trying to access greenplum table using pyspark 使用Python加密和解密AWS秘密/访问密钥 - Encrypting and decrypting AWS secret/access keys using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM