【问题标题】:Parameter validation failed: Unknown parameter in input: "Expires"参数验证失败:输入中的未知参数:“过期”
【发布时间】:2021-08-23 20:33:26
【问题描述】:

我有这个 lambda 函数,它为存储桶中的对象创建一个签名的 URL,并将一个 URL 返回到 API 网关。

这是我的 lambda 函数代码

import uuid
import boto3


def lambda_handler(event, context):
    s3 = boto3.client('s3')

    upload_key = uuid.uuid4().hex

    bucket = 'test-bucket-athena'


    # Generate the presigned URL for upload
    presigned_upload_url = s3.generate_presigned_url(
        ClientMethod='put_object',
        Params={
            'Bucket': bucket,
            'Key': upload_key,
            'Expires': 3600
        }
    )

    # return the result
    return {
        "upload_url": presigned_upload_url
        #"download_url": presigned_download_url
    }

当我运行这段代码时,我得到以下错误

[ERROR] ParamValidationError:参数验证失败:输入中的未知参数:“Expires”,必须是以下之一:Bucket、IfMatch、 IfModifiedSince, IfNoneMatch, IfUnmodifiedSince, 键, 范围, ResponseCacheControl、ResponseContentDisposition、 响应内容编码,响应内容语言,响应内容类型, ResponseExpires、VersionId、SSECustomerAlgorithm、SSECustomerKey、 SSECustomerKeyMD5、RequestPayer、PartNumber、ExpectedBucketOwner Traceback(最近一次调用最后一次):文件 “/var/task/lambda_function.py”,第 14 行,在 lambda_handler 中 presigned_download_url = s3.generate_presigned_url(文件“/var/runtime/botocore/signers.py”,第 586 行,在 generate_presigned_url request_dict = serializer.serialize_to_request(文件“/var/runtime/botocore/validate.py”,第 293 行,在 serialize_to_request 引发 ParamValidationError(report=report.generate_report())

我认为我提供的 S3 网址格式不正确,但我测试了所有组合,但似乎没有任何效果

我尝试过以下格式

bucket = 'test-bucket-athena'
bucket = 'test-bucket-athena/'
bucket = 's3://test-bucket-athena/'
bucket = 's3://test-bucket-athena'
bucket = 's3:///test-bucket-athena'

请建议我在这里犯了什么愚蠢的错误

【问题讨论】:

  • 您的代码适用于我,使用我自己的存储桶,格式为 bucket = 'test-bucket-athena';不知何故,它认为 Expires 不属于您的参数,但不知道为什么
  • 你用的是哪个boto3版本?
  • 嗨 @JonathanLeon 我使用的是 1.17.88 boto3 版本
  • 我在 '1.16.30' 上,该方法看起来与您的版本相同。 generate_presigned_url(ClientMethod, Params=None, ExpiresIn=3600, HttpMethod=None) 虽然这使用 ExpiresIn 而不是 Expires。也许把它从 params 中拿出来和 Params 和 ClientMethod 一样的级别????
  • 当我删除 'ExpiresIn': 3600 或 Expires 时,我可以得到相应的 url

标签: python-3.x amazon-web-services amazon-s3 aws-lambda


【解决方案1】:

bucketname 的格式应为“bucketname”,就像您在第一行中所做的那样:

bucket = 'test-bucket-athena'

但是,我认为您错过了错误消息中的错误:

[ERROR] ParamValidationError: Parameter validation failed: Unknown parameter in input: "Expires", must be one of:

您在 Params 部分中有 Expires,它实际上应该与 Params 处于同一级别。

presigned_upload_url = s3.generate_presigned_url(
        ClientMethod='put_object',
        Params={
            'Bucket': bucket,
            'Key': upload_key
        }, 
        ExpiresIn=3600
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-23
    • 2017-06-27
    • 2019-05-16
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多