首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无服务器框架-在S3中创建一个Lambda和S3并上传一个文件。然后,使用Lambda解压到DynamoDB

无服务器框架-在S3中创建一个Lambda和S3并上传一个文件。然后,使用Lambda解压到DynamoDB
EN

Stack Overflow用户
提问于 2020-05-14 23:40:57
回答 4查看 1.2K关注 0票数 1

这是我第一次使用无服务器框架,我的任务是用无服务器创建一个lambda、s3和dynamodb,然后调用lambda将其从s3转换到dynamo。我正在尝试获得一个由serverless生成的名称到我的S3中,以便在我的Lambda中使用它,但我没有运气。这是我的serveless.yml的样子:

代码语言:javascript
运行
复制
service: fetch-file-and-store-in-s3

frameworkVersion: ">=1.1.0"

custom:
  bucket: 
    Ref: Outputs.AttachmentsBucketName

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
        - s3:PutObjectAcl
      Resource: "arn:aws:s3:::${self:custom.bucket.Ref}/*"

functions:
  save:
    handler: handler.save
    environment:
      BUCKET: ${self:custom.bucket.Ref}

resources:
  # S3
  AttachmentsBucket:
  Type: AWS::S3::Bucket
  Properties:
    # Set the CORS policy
    CorsConfiguration:
      CorsRules:
        -
          AllowedOrigins:
            - '*'
          AllowedHeaders:
            - '*'
          AllowedMethods:
            - GET
            - PUT
            - POST
            - DELETE
            - HEAD
          MaxAge: 3000

  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket

下面是它创建s3存储桶的部分

代码语言:javascript
运行
复制
Resources:
    # S3
    AttachmentsBucket:
      Type: AWS::S3::Bucket
      Properties:
        # Set the CORS policy
        CorsConfiguration:
          CorsRules:
            - AllowedOrigins:
                - '*'
            - AllowedHeaders:
                - '*'
            - AllowedMethods:
                - GET
                - PUT
                - POST
                - DELETE
                - HEAD
            - MaxAge: 3000
  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket

这是我目前得到的错误:

代码语言:javascript
运行
复制
λ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service fetch-file-and-store-in-s3.zip file to S3 (7.32 MB)...
Serverless: Validating template...

  Error --------------------------------------------------

  Error: The CloudFormation template is invalid: Invalid template property or properties [AttachmentsBucket, Type, Properties]
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-05-18 01:07:35

我想出了一个解决方案。因为我是新手,而且这是我的第一个项目,一开始我对术语不是很熟悉。我所做的就是在这里给我的存储桶命名:

代码语言:javascript
运行
复制
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: ${self:custom.bucket} # Getting the name of table I defined under custom in serverless.yml
  # Make Bucket publicly accessable
  MyBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
        Bucket: !Ref Bucket
        PolicyDocument:
          Statement:
            - Effect: Allow
              Principal: '*' # public access to access the bucket files 
              Action: s3:GetObject
              Resource: 'arn:aws:s3:::${self:custom.bucket}/*'

然后,为了使用deploy上传文件,我找到了一个名为serverless-s3bucket-sync的插件,并将其添加到自定义属性和文件夹下的文件位置:

代码语言:javascript
运行
复制
custom:
  bucket: mybucketuniquename #unique global name it will create for the bucket
  s3-sync: 
      - folder: images
        bucket: ${self:custom.bucket}

并添加了IamRole:

代码语言:javascript
运行
复制
iamRoleStatements:
    #S3 Permissions
    - Effect: Allow
      Action:
        - s3:*
      Resource: "arn:aws:s3:::${self:custom.bucket}"
票数 0
EN

Stack Overflow用户

发布于 2020-05-15 02:50:30

缩进有一些问题:

代码语言:javascript
运行
复制
resources:
  Resources:
    # S3
    AttachmentsBucket:
      Type: AWS::S3::Bucket
      Properties:
        # Set the CORS policy
        CorsConfiguration:
          CorsRules:
            - AllowedOrigins:
                - '*'
            - AllowedHeaders:
                - '*'
            - AllowedMethods:
                - GET
                - PUT
                - POST
                - DELETE
                - HEAD
            - MaxAge: 3000
  # Print out the name of the bucket that is created
  Outputs:
    AttachmentsBucketName:
      Value:
        Ref: AttachmentsBucket
票数 1
EN

Stack Overflow用户

发布于 2020-05-15 03:11:52

缩进对于serverless.yml文件很重要。在本例中,AttachmentsBucket是一个资源,它应该在Resources下的子部分中有一个制表符空格,然后TypeProperties应该在资源名称:AttachmentsBucket中有一个制表符空格,而在提供的示例中实际上有两个制表符空格。CloudFormation将无法处理此特定资源,因为它无法识别具有正确名称和属性资源。

请参阅更新后的示例:

代码语言:javascript
运行
复制
Resources:
  AttachmentsBucket:
    Type: AWS::S3::Bucket
    Properties:
    # Set the CORS policy
    CorsConfiguration:
      CorsRules:
        - AllowedOrigins:
            - '*'
        - AllowedHeaders:
            - '*'
        - AllowedMethods:
            - GET
            - PUT
            - POST
            - DELETE
            - HEAD
        - MaxAge: 3000

# Print out the name of the bucket that is created
Outputs:
  AttachmentsBucketName:
    Value: !Ref AttachmentsBucket
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61801520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档