如何使用 boto3 从 Amazon S3 获取 .stl 文件?
How to get .stl file from Amazon S3 by using boto3?
我有一个 Django Web 应用程序并将其部署到 Elastic Beanstalk 环境。我也有 numpy-stl 包。我正在尝试从 Amazon S3 存储桶中获取一个 .stl 文件,并将此文件与 stl 包的功能一起使用,但我收到了诸如 'bytes' object has no attribute 'get_mass_properties'
.
之类的错误
我的代码是;
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=key)
body = obj['Body'].read()
volume, cog, inertia = body.get_mass_properties()
如何获取 .stl 文件并使用它?
假设您正在谈论这种 stl 文件格式,一旦您从 S3 将其读入 python,您需要一些 python 库来打开它。
快速搜索returns numpy-stl:
Simple library to make working with STL files (and 3D objects in general) fast and easy.
因此您可以安装该库并尝试在您正在下载的文件上使用它。
如果您 运行 您的代码在 lambda 上(没有写在您的问题中?)那么您必须将库与您的部署包捆绑在一起或为此构建自定义 lambda 层。
我已修复如下。
import stl
import boto3
import tempfile
s3 = boto3.resource('s3', region_name=region)
bucket = s3.Bucket(bucket)
obj = bucket.Object(uploadedVolume)
tmp = tempfile.NamedTemporaryFile()
with open(tmp.name, 'wb') as f:
obj.download_fileobj(f)
stlMesh = stl.mesh.Mesh.from_file(tmp.name)
volume, cog, inertia = stlMesh.get_mass_properties()
我有一个 Django Web 应用程序并将其部署到 Elastic Beanstalk 环境。我也有 numpy-stl 包。我正在尝试从 Amazon S3 存储桶中获取一个 .stl 文件,并将此文件与 stl 包的功能一起使用,但我收到了诸如 'bytes' object has no attribute 'get_mass_properties'
.
我的代码是;
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=key)
body = obj['Body'].read()
volume, cog, inertia = body.get_mass_properties()
如何获取 .stl 文件并使用它?
假设您正在谈论这种 stl 文件格式,一旦您从 S3 将其读入 python,您需要一些 python 库来打开它。
快速搜索returns numpy-stl:
Simple library to make working with STL files (and 3D objects in general) fast and easy.
因此您可以安装该库并尝试在您正在下载的文件上使用它。
如果您 运行 您的代码在 lambda 上(没有写在您的问题中?)那么您必须将库与您的部署包捆绑在一起或为此构建自定义 lambda 层。
我已修复如下。
import stl
import boto3
import tempfile
s3 = boto3.resource('s3', region_name=region)
bucket = s3.Bucket(bucket)
obj = bucket.Object(uploadedVolume)
tmp = tempfile.NamedTemporaryFile()
with open(tmp.name, 'wb') as f:
obj.download_fileobj(f)
stlMesh = stl.mesh.Mesh.from_file(tmp.name)
volume, cog, inertia = stlMesh.get_mass_properties()