如何使用 boto3 和 read() 其内容从 S3 访问项目
How to access an item from S3 using boto3 and read() its contents
我有一种方法可以从 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 存储桶访问对象并将其转换为图像,就像上面的方法一样。但是,我不确定如何使用 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 从存储桶访问特定项目?
- 有没有办法
read
访问项目的内容类似于我在 my_method
中使用 req.read()
所做的?
我会这样 1:
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,我没试过 this SO answer 建议:
body = obj.get()['Body'].read()
使用 boto3
提出的高级 ressource
。
我有一种方法可以从 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 存储桶访问对象并将其转换为图像,就像上面的方法一样。但是,我不确定如何使用 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 从存储桶访问特定项目?
- 有没有办法
read
访问项目的内容类似于我在my_method
中使用req.read()
所做的?
我会这样 1:
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,我没试过 this SO answer 建议:
body = obj.get()['Body'].read()
使用 boto3
提出的高级 ressource
。