【问题标题】:Lambda: Amazon s3 direct upload error signature does not matchLambda:Amazon s3 直接上传错误签名不匹配
【发布时间】:2016-12-30 08:25:25
【问题描述】:

我想通过使用预签名的 URL 将图像文件上传到 AWS s3 存储桶,但是屏幕截图中显示了一个错误,我已关注此页面上的帖子s3 direct file upload,我想知道我在犯什么错误,而且我想知道这是否是服务器端问题,或者我应该使用一些不同的方法来向“预签名”URL 发出 put 请求,谢谢。

我的 serverless.yml

service: my-service-api

provider:
  name: aws
  runtime: nodejs4.3
  stage:  dev
  region: us-east-1
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "dynamodb:*"        
      Resource: "*"
    - Effect: "Allow"
      Action:
        - "s3:*"        
      Resource: "arn:aws:s3:::profile-images/*"   

custom:
  globalResultTtlInSeconds: 1

package:
  individually: true
  include:
    - node_modules/mysql/**
    - node_modules/bluebird/**
    - node_modules/joi/**
  exclude:
    - .git/**
    - .bin/**
    - tmp/**
    - api/**
    - node_modules/**
    - utils/**
    - package.json
    - npm_link.sh
    - templates.yml

functions:
  profiles:
    handler: api/profiles/handler.profiles
    events:
      - http:
          method: POST
          path: api/profiles/uploadURL
          cors: true
          integration: lambda
          request: ${file(./templates.yml):request}  
          authorizer: 
            arn: arn:aws:lambda:us-east-1:000000000000:function:customAuthorizer
            resultTtlInSeconds: ${self:custom.globalResultTtlInSeconds}
            identitySource: method.request.header.Authorization
    package:
      include:
        - api/profiles/**
        - node_modules/node-uuid/**
        - node_modules/jsonwebtoken/**
        - node_modules/rand-token/**          

resources:
  Resources:
    UploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: profile-images
        AccessControl: PublicRead
        CorsConfiguration:
          CorsRules:
          - AllowedMethods:
            - GET
            - PUT
            - POST
            - HEAD
            AllowedOrigins:
            - "*"
            AllowedHeaders:
            - "*"   
    IamPolicyInvokeLambdaFunction:
      Type: AWS::IAM::Policy     
      Properties: 
        PolicyName: "lambda-invoke-function"
        Roles:
          - {"Ref" : "IamRoleLambdaExecution"}
        PolicyDocument:
          Version: '2012-10-17'
          Statement:            
              - Effect: Allow
                Action: 
                  - "lambda:InvokeFunction"
                Resource: "*"

我的处理程序文件

var s3Params = {
                Bucket: 'profile-images',
                Key: image.name,
                ACL: 'public-read'
            };

 s3.getSignedUrl('putObject', s3Params, function (err, url){
       if(err){
          console.log('presignedURL err:',err);
          context.succeed({error: err});
       }
       else{
          console.log('presignedURL: ',url);
          context.succeed({uploadURL: url});                                           
       }                    
  });

【问题讨论】:

  • 为什么在 lambda 函数中上传图片需要一个签名的 url。或者,您可以拥有运行 Lambda 函数的角色以访问 S3 存储桶,这应该足够了
  • 谢谢@Rajesh,你能给我一些例子吗?这将有助于我理解
  • @Rajesh 检查链接的博客文章。这里的目标似乎是使用“服务器”生成的预签名 URL 从网页拖放/上传到 S3,这实际上是一个同步 Lambda 函数。从 Lambda 上传文件不适用。
  • @Balkrishna 在测试工具中选择 form-data 并使用 multupart/form-data 作为 Content-Type 对于 PUT 没有意义。您需要原始上传,并且需要将 Content-Type 设置为正在上传的对象的真实 MIME 类型。我不确定这是否是唯一的问题,但绝对行不通。
  • @Balkrishna 签名 URL 也具有时间有效性。您能否确保您的函数在时间范围内将图像上传到 S3。例如:如果预签名 URL 的有效期为 30 秒,并且您的图像上传在 30 秒后开始,您可能会收到无效签名异常。

标签: node.js amazon-web-services amazon-s3 aws-lambda pre-signed-url


【解决方案1】:

在这个问题上花了更多时间后,我意识到这不是服务器端的问题,而是发出请求的问题。我需要为我的 PUT 请求设置标头,因为当 AWS s3 收到任何请求时,它会检查该请求的签名与标头,因此如果您在创建 preSignedURL 时设置“ContentType”或“ACL”,那么您必须提供“Content-在您的请求中输入'和'x-amz-acl'。

这是我更新的“s3Params”

var s3Params = {
                Bucket: 'profile-images',
                Key: image.name,
                ACL: 'public-read',
                ContentType: image.type
            };

这是我的要求

最后我从这篇帖子set headers for presigned PUT s3 requests得到了一些帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2019-06-01
    相关资源
    最近更新 更多