简体   繁体   中英

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM