How to configure authorization mechanism inline with boto3

后端 未结 2 857
悲哀的现实
悲哀的现实 2021-02-04 06:25

I am using boto3 in aws lambda to fecth object in S3 located in Frankfurt Region.

v4 is necessary. otherwise following error will return

\"errorMessage\"         


        
相关标签:
2条回答
  • 2021-02-04 06:56

    I tried the session approach, but I had issues. This method worked better for me, your mileage may vary:

    s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))
    

    You will need to import Config from botocore.client in order to make this work. See below for a functional method to test a bucket (list objects). This assumes you are running it from an environment where your authentication is managed, such as Amazon EC2 or Lambda with a IAM Role:

    import boto3
    from botocore.client import Config
    from botocore.exceptions import ClientError
    
    def test_bucket(bucket):
        print 'testing bucket: ' + bucket
        try:
            s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))
            b = s3.Bucket(bucket)
            objects = b.objects.all()
    
            for obj in objects:
                print obj.key
            print 'bucket test SUCCESS'
        except ClientError as e:
            print 'Client Error'
            print e
            print 'bucket test FAIL'
    

    To test it, simply call the method with a bucket name. Your role will have to grant proper permissions.

    0 讨论(0)
  • 2021-02-04 07:05

    Instead of using the default session, try using custom session and Config from boto3.session

    import boto3
    import boto3.session
    session = boto3.session.Session(region_name='eu-central-1')
    s3client = session.client('s3', config= boto3.session.Config(signature_version='s3v4'))
    s3client.get_object(Bucket='<Bkt-Name>', Key='S3-Object-Key')
    
    0 讨论(0)
提交回复
热议问题