Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

s3.Object() throws TypeError: sequence item 0: expected str instance, tuple found

Following the boto3 documentation, I tried doing this:

session = boto3.Session(
aws_access_key_id=KEY,
aws_secret_access_key=SECRET_KEY
)
s3 = session.resource('s3')

obj = s3.Object('test', 'test/myfile.csv')

And when I run obj.get(), I receive:

TypeError: sequence item 0: expected str instance, tuple found

I need to get contents of this csv file.

It works when I try the same using client instead of session:

s3 = boto3.client('s3')
obj = s3.get_object(Bucket='test', Key='test/myfile.csv')
like image 874
Desiigner Avatar asked May 08 '26 11:05

Desiigner


1 Answers

This question applies to both AWS S3 buckets and DigitalOcean Spaces buckets.

To replicate the error:

import boto3

KEY = '********************',
SECRET_KEY = '*******************************************'

# Initialize a session using DigitalOcean Spaces.
session = boto3.session.Session()

client = session.client('s3', region_name='sgp1',
                        endpoint_url='https://sgp1.digitaloceanspaces.com',
                        aws_access_key_id=KEY,
                        aws_secret_access_key=SECRET_KEY)

throws the error:

~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/botocore/auth.py in scope(self, request)
    322         scope.append(self._service_name)
    323         scope.append('aws4_request')
--> 324         return '/'.join(scope)
    325 
    326     def credential_scope(self, request):

TypeError: sequence item 0: expected str instance, tuple found

It looks like there's something wrong with the API but if we look carefully,

>>>print(type(SECRET_KEY))
str

>>> print(type(KEY))
tuple

As commented by michael-sqlbot, there is a tuple type in one of the auth key.

Note the comma, in the code above at:

import boto3

KEY = '********************',

Simply change it a string type by removing the comma:

import boto3

KEY = '********************'
SECRET_KEY = '*******************************************'

# Initialize a session using DigitalOcean Spaces.
session = boto3.session.Session()

client = session.client('s3', region_name='sgp1',
                        endpoint_url='https://sgp1.digitaloceanspaces.com',
                        aws_access_key_id=KEY,
                        aws_secret_access_key=SECRET_KEY)

[out]:

{'ResponseMetadata': {'RequestId': '*************',
  'HostId': '',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'transfer-encoding': 'chunked',
   'x-amz-request-id': '*************',
   'content-type': 'application/xml',
   'date': 'Mon, 15 Jun 2020 01:14:42 GMT',
   'strict-transport-security': 'max-age=15552000; includeSubDomains; preload'},
  'RetryAttempts': 0},
 'Buckets': [{'Name': '*****',
   'CreationDate': datetime.datetime(2019, 11, 20, 8, 28, 36, 548000, tzinfo=tzutc())}],
 'Owner': {'DisplayName': '*******', 'ID': '********'}}
like image 57
alvas Avatar answered May 11 '26 00:05

alvas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!