小编sri*_*ram的帖子

Boto3 对象读取类型错误:预期字符串或类似字节的对象

我正在尝试读取上传到 Amazon s3 存储桶的 .txt 文件中的文本。但我收到错误。

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()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误。

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, …
Run Code Online (Sandbox Code Playgroud)

amazon-s3

10
推荐指数
2
解决办法
3万
查看次数

标签 统计

amazon-s3 ×1