Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在我的lambda中访问通过cloudformation创建的bucket?_Python_Amazon Web Services_Aws Lambda - Fatal编程技术网

Python 如何在我的lambda中访问通过cloudformation创建的bucket?

Python 如何在我的lambda中访问通过cloudformation创建的bucket?,python,amazon-web-services,aws-lambda,Python,Amazon Web Services,Aws Lambda,我正在通过cloudformation模板创建一个lambda和一个S3。对于bucket,我为bucketName传入一个参数。我想在我的python代码中为lambda函数访问这个bucket。我该怎么做 云形成模板 AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Sample SAM Template Globals: Function: Ti

我正在通过cloudformation模板创建一个lambda和一个S3。对于bucket,我为bucketName传入一个参数。我想在我的python代码中为lambda函数访问这个bucket。我该怎么做

云形成模板

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template 

Globals:
  Function:
    Timeout: 3

Parameters:
  bucketName:
     Default: somename
Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: PublicRead
      BucketName: !Sub '${bucketName}-${AWS::AccountID}'

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
    Environment:
      Variables:
        s3bucketName: !GetAtt S3Bucket
      
lambda_处理器函数

import boto3
import os
from requests_aws4auth import AWS4Auth

session = boto3.Session()
credentials = session.get_credentials()
aws4auth = AWS4Auth(credentials.access_key,credentials.secret_key,region, service, session_token=credentials.token)
                    
s3 = boto3.resource('s3')

//get access to the S3 bucket created??

s3=boto3.resource('s3'))
obj=s3.对象(bucket,key)
objAsString=obj.get()['Body'].read().decode('utf-8')
您还缺少从lambda访问s3存储桶的策略

Variables:
   S3_BUCKET_NAME: !ImportValue s3-bucket-name

HelloWorldFunction:
类型:AWS::Serverless::Function
特性:
你好,世界/
处理程序:app.lambda\u处理程序
运行时:python3.8
环境:
变量:
S3_BUCKET_NAME:!参考S3铲斗
政策:
-声明:
-效果:
行动:
-'s3:GetObject'
-'s3:ListBucket'
-'s3:ListBucketMultiportupLoads'
-'s3:PutObject'
资源:
- !子'arn:aws:s3::${S3Bucket}'
我将在单独的cloudformation
s3.yaml中移动bucket的创建,并执行一次

。。。。
资源:
S3Bucket:
类型:AWS::S3::Bucket
特性:
访问控制:PublicRead
BucketName:!子“${bucketName}-${AWS::AccountID}”
产出:
BucketName:
值:!参考S3铲斗
描述:“Bucket name”
出口:
名称:s3 bucket Name
之后,我可以用lambda在其他cloudformation中导入这个
s3 bucket name

Variables:
   S3_BUCKET_NAME: !ImportValue s3-bucket-name


最常见的选项是通过环境变量向Lambda函数提供bucket名称。添加到您的
AWS::Serverless::Function
@jarmod-更新了我的答案。这应该行吗?看起来很合理。我通常会把它命名为S3_BUCKET_name。@jarmod-谢谢,我只想问最后一个问题。-我是否需要确保s3是在lambda之前创建的。我的python代码->keyError'S3\u BUCKET\u NAME'中出现错误,我在代码中使用os.environ['S3\u BUCKET\u NAME']来访问它ozil 1分钟前编辑如果Lambda函数中发生Python错误,则环境变量未正确设置。使用控制台查看CloudFormation堆栈实际创建了哪些环境变量。我更新了答案,在enivormentVariables下添加了bucket名称。一旦我让s3访问lambda,就像你说的。此设置有效吗?是,bucket name可以作为env变量访问。我会使用
!Ref
而不是
!GetAttr
<代码>S3\u桶\u名称:!参考S3Bucket
,更新了我答案中的示例。最后一个问题-我是否需要确保在lambda之前创建s3。我的python代码->keyError'S3\u BUCKET\u NAME'中有一个错误,我在代码中使用os.environ['S3\u BUCKET\u NAME']来访问它。是的,您需要先创建它。您可以使用单独的cloudformation创建它,或者使用
DependsOn
,因此lambda依赖于首先创建bucket。