How to get .stl file from Amazon S3 by using boto3?

307 Views Asked by At

I have a Django Web application and i deployed it to Elastic Beanstalk environment. I also have the numpy-stl package. I'm trying to get a .stl file from Amazon S3 bucket and use this file with a stl package's function but i'm getting an error such as 'bytes' object has no attribute 'get_mass_properties'.

My code is;

s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=key)
body = obj['Body'].read()
volume, cog, inertia = body.get_mass_properties()

How can i get the .stl file and use it?

2

There are 2 best solutions below

0
On BEST ANSWER

I have fixed such as below.

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()
3
On

Assuming that you are talking about this stl file format, once you read it in into python from S3, you need some python library to open it.

Quick search returns numpy-stl:

Simple library to make working with STL files (and 3D objects in general) fast and easy.

Thus you can install that library and attempt to use it on the file you are downloading.

In case you run your code on lambda (not written in your question?) then you would have to bundle the library with your deployment package or construct custom lambda layer for that.