Amazon AWS Cognito and Python Boto3 to establish AWS connection and upload file to Bucket

前端 未结 3 961
[愿得一人]
[愿得一人] 2021-02-06 08:57

I\'m trying to use the AWS cognito service to authenticate and upload a file. I have been provided my regionType, identityPool, AWS account ID, and UnAuthRole. I also know the p

相关标签:
3条回答
  • 2021-02-06 09:26

    PhilBot, I don't know why your original code sample connects to s3 using boto (as opposed to boto3). The code connects to cognito using boto3. As of now, boto3 is stable and there's probably not much reason to use boto anymore. (Maybe when you originally posted your question, boto3 was not as stable as it is today.)

    When I tried using your code to connect to kinesis with boto3, it didn't work -- I had to pass response["Credentials"]["SessionToken"] as the aws_session_token to the client() function.

    0 讨论(0)
  • 2021-02-06 09:32

    This question is really invalid because the authentication was failing not on creating a session but when trying to list the buckets.

    Uploading and downloading from a specific bucket works fine with the above code but not the listing of all buckets.

    # Upload a new file
    data = open('test.jpg', 'rb')
    s3.Bucket('mybucket').put_object(Key='test.jpg', Body=data)
    
    # S3 Object
    obj = s3.Object(bucket_name='mybucket', key='test.jpg')
    response = obj.get()
    data = response['Body'].read()
    print len(data)
    
    0 讨论(0)
  • 2021-02-06 09:33

    This is your error:

    File "./test.py", line 32, in <module>
    bucket = conn.get_bucket("elektradevbucket")
    

    This is your part of the code that references the bucket:

    bucket = conn.get_bucket("testbucket")
    '''
    s3 = boto3.resource('s3')
    for bucket in s3.buckets.all():
        print(bucket.name)
    s3.Bucket('testbucket')
    

    Are you sure you are running or calling the correct script?

    Best, -Iulian

    0 讨论(0)
提交回复
热议问题