How to specify credentials when connecting to boto3 S3?

后端 未结 5 2087
我在风中等你
我在风中等你 2020-12-04 08:10

On boto I used to specify my credentials when connecting to S3 in such a way:

import boto
from boto.s3.connection import Key, S3Connection
S3 = S3Connection(         


        
5条回答
  •  粉色の甜心
    2020-12-04 08:49

    This is older but placing this here for my reference too. boto3.resource is just implementing the default Session, you can pass through boto3.resource session details.

    Help on function resource in module boto3:
    
    resource(*args, **kwargs)
        Create a resource service client by name using the default session.
    
        See :py:meth:`boto3.session.Session.resource`.
    

    https://github.com/boto/boto3/blob/86392b5ca26da57ce6a776365a52d3cab8487d60/boto3/session.py#L265

    you can see that it just takes the same arguments as Boto3.Session

    import boto3
    S3 = boto3.resource('s3', region_name='us-west-2', aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY)
    S3.Object( bucket_name, key_name ).delete()
    

提交回复
热议问题