Home > Blockchain >  Python-Boto3 to S3 limitation
Python-Boto3 to S3 limitation

Time:09-24

Im new to Python and for my project purpose and Im using using boto3 to access AWS S3 in a pycharm IDE

I completed package installation for boto3 ,pyboto then created a Python file and successfully created bucket and transferred the files to S3 from my local using boto3

Later i created another python file in the same working directory and using the same steps but this time Im not able to connect AWS and not even API calls Im getting

So am doubtful that whether we can use boto3 packages with only one python file and we cant use it another python file in same directory?

I tried by creating both s3 client and s3 resource but no luck

Please advice is there any limitations is there for boto3 ?

Below are the Python code:-

import boto3
import OS

bucket_name='*****'

def s3_client():
    s3=boto3.client('s3')
    """:type:pyboto3:s3"""
    return s3

def s3_resource():
    s3=boto3.resource('s3')
    return s3

def create_bucket(bucket_name):
    val=s3_client().create_bucket(=bucket_name,

        CreateBucketConfiguration={
            'LocationConstraint':'ap-south-1'
        })
    return val




def upload_file():
    s3=s3_resource().meta.client.upload_file('d:/s3_load2.csv',bucket_name,'snowflake.csv')
    return s3

def upload_small_file():

    s3=s3_client().upload_file('d:/s3_load2.csv',bucket_name,'snowflake.csv')
    return s3




def create_bucket(bucket_name):

    val=s3_client().create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={
            'LocationConstraint':'ap-south-1'
        })
    return val


#calling 

upload_small_file()

CodePudding user response:

Perhaps the AWS credentials weren't set in the environment where you run the 2nd script. Or maybe the credentials you were using while running the 1st script already expired. Try getting your AWS credentials and set them when you instantiate a boto3 client or resource as documented:

import boto3

client = boto3.client(
    's3',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    aws_session_token=SESSION_TOKEN  # This is only required for temporary credentials
)

Or you can also try setting them as environment variables.

export AWS_ACCESS_KEY_ID="some key"
export AWS_SECRET_ACCESS_KEY="some key"
export AWS_SESSION_TOKEN="some token"  # This is only required for temporary credentials

Or as a configuration file. See the docs for the complete list.

  • Related