Python 如何使用boto3和read()从S3访问项目及其内容

Python 如何使用boto3和read()从S3访问项目及其内容,python,numpy,amazon-s3,boto3,Python,Numpy,Amazon S3,Boto3,我有一个方法,可以从URL获取文件并将其转换为OpenCV图像 def my_method(self, imgurl): req = urllib.urlopen(imgurl) r = req.read() arr = np.asarray(bytearray(r), dtype=np.uint8) image = cv2.imdecode(arr,-1) # 'load it as it is' return image 我想使用boto3从s3 bucket

我有一个方法,可以从URL获取文件并将其转换为OpenCV图像

def my_method(self, imgurl):
   req = urllib.urlopen(imgurl)
   r = req.read()
   arr = np.asarray(bytearray(r), dtype=np.uint8)
   image = cv2.imdecode(arr,-1) # 'load it as it is'
   return image
我想使用boto3从s3 bucket访问一个对象,并将其转换为图像,就像上面的方法一样。但是,我不知道如何使用boto3从存储桶访问项目,然后进一步了解如何
read()
读取该项目的内容

下面是我试过的

>>> import botocore
>>> import boto3
>>> client = boto3.client('s3',aws_access_key_id="myaccsskey",aws_secret_access_key="secretkey")
>>> bucketname = "mybucket"
>>> itemname = "demo.png"
问题

  • 如何使用boto3从桶中访问特定项目
  • 是否有一种方法可以使用
    req.read()
    读取所访问项目的内容,类似于我在
    my_方法中所做的操作
  • 我会这样做:

    import boto3
    s3 = boto3.resource('s3',
                         use_ssl=False,
                         endpoint_url="http://localhost:4567",
                         aws_access_key_id="",
                         aws_secret_access_key="",
    )
    obj = s3.Object(bucketname, itemname)
    
    对于2,我从未尝试过建议:

    使用由
    boto3

    提出的高级
    ressource
    ,我会这样做:

    import boto3
    s3 = boto3.resource('s3',
                         use_ssl=False,
                         endpoint_url="http://localhost:4567",
                         aws_access_key_id="",
                         aws_secret_access_key="",
    )
    obj = s3.Object(bucketname, itemname)
    
    对于2,我从未尝试过建议:

    使用由
    boto3
    提出的高级
    ressource