Skip to main content
 首页 » 编程设计

python之AWS Lambda之time.time() 返回未定义

2025年01月19日11cloudgamer

我正在使用 lambda 函数,我使用 boto3 将 item() 放入我的 DynamoBD 表中,并在代码中添加 ttl 参数(生存时间)。

ttl = str(int(time.time() + 2629746)) 

这一行给了我 1 个月的时间,但出于某种原因我收到了很多这样的错误:

An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: undefined: ClientError 
Traceback (most recent call last): 
File "/var/task/lambda_function.py", line 52, in handler 
response = c_put_item(d) 
File "/var/task/lambda_function.py", line 40, in c_put_item 
'ttl':{'N':ttl} 
File "/var/runtime/botocore/client.py", line 317, in _api_call 
return self._make_api_call(operation_name, kwargs) 
File "/var/runtime/botocore/client.py", line 615, in _make_api_call 
raise error_class(parsed_response, operation_name) 
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: undefined 

知道为什么吗?

PS:我用的是python3

-- 编辑:

我添加了更多的代码。 由于某种原因,这不起作用。

    ttl = str(int(time.time() + 2629746)) 
 
    response = client.put_item(TableName='MYTABLENAME',Item={ 
        'item':{'S':item}, 
        'title':{'S':title}, 
        'link':{'S':link}, 
        'price':{'N':price}, 
        'category':{'S':category}, 
        'avaliable':{'S':avaliable}, 
        'image':{'S':image}, 
        'ttl':{'N':ttl} 
    }) 

-- 编辑 2:

AWS docs指定你应该像我一样使用 put_item() 除了我被迫使用 str() 因为我收到错误。

请您参考如下方法:

@kichik 的评论实际上是正确的。此错误不一定表示 ttl 为 None。它表示整个调用的字段之一是 None。

因此,我检测到冲突字段时添加了异常,问题停止了。