Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 Object Read Type error: expected string or bytes-like object

Tags:

amazon-s3

I am trying to read text that is in a .txt file that is uploaded to the Amazon s3 Bucket. But I am getting an error.

import boto3
import json

s3= boto3.resource('s3')
bucketList=[]
i=1
for bucket in s3.buckets.all():
           print(str(i)+'.' + bucket.name)
           bucketList.append(bucket)
           i=i+1
BucketSel=int(input('Select from the bucket list using number'))
AccessBucket=bucketList[BucketSel-1]
FileList=[]
for obj in AccessBucket.objects.all():
           print(obj.key)
           FileList.append(obj.key)
FileSel=int(input('Select the File from the List using number'))
AccessFile=FileList[FileSel-1]
obj=s3.Object(AccessBucket, AccessFile)
str=obj.get()['Body'].read()

I am getting this following Error.

Traceback (most recent call last):
  File "G:\pyworkspace\BucketSelectFromList.py", line 20, in <module>
    str=obj.get()['Body'].read()
  File "C:\Program Files\Python37\lib\site-packages\boto3\resources\factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\boto3\resources\action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "C:\Program Files\Python37\lib\site-packages\botocore\client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Program Files\Python37\lib\site-packages\botocore\client.py", line 586, in _make_api_call
    api_params, operation_model, context=request_context)
  File "C:\Program Files\Python37\lib\site-packages\botocore\client.py", line 619, in _convert_to_request_dict
    api_params, operation_model, context)
  File "C:\Program Files\Python37\lib\site-packages\botocore\client.py", line 648, in _emit_api_params
    params=api_params, model=operation_model, context=context)
  File "C:\Program Files\Python37\lib\site-packages\botocore\hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "C:\Program Files\Python37\lib\site-packages\botocore\hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "C:\Program Files\Python37\lib\site-packages\botocore\handlers.py", line 217, in validate_bucket_name
    if VALID_BUCKET.search(bucket) is None:
TypeError: expected string or bytes-like object

I understand that the error occurs at the read() but I am not sure what to do rectify this error.


Solved

I used obj=s3.Object(AccessBucket.name, AccessFile) instead of obj=s3.Object(AccessBucket, AccessFile) and it's working.

like image 207
srivatsan sozhavaram Avatar asked Apr 01 '26 21:04

srivatsan sozhavaram


2 Answers

I had a similar problem when bucket name was configured incorrectly.

like image 131
Neal Ratan Avatar answered Apr 03 '26 10:04

Neal Ratan


My error was much more stupid:

bucket = f"{client}-media",

and that comma at the end made bucket a non-string.

like image 24
Putnik Avatar answered Apr 03 '26 09:04

Putnik