is there anyway to load/read an external file(i.e, AWS S3) in numpy?. I have several npy files stored in S3. I have tried to access them through a S3 presigned url but it seems neither numpy.load method or np.genfromtxt are able to read them.
I wouldn't want to save files on local file system and then load them on numpy.
Any idea?
Using s3fs
import numpy as np
from s3fs.core import S3FileSystem
s3 = S3FileSystem()
key = 'your_file.npy'
bucket = 'your_bucket'
df = np.load(s3.open('{}/{}'.format(bucket, key)))
You might have to set the allow_pickle=True
depending on your file to be read.
I've compared s3fs and io.BytesIO for loading a 28G npz file from s3. s3fs takes 30 min while io takes 12 min.
obj = s3_session.resource("s3").Object(bucket, key)
with io.BytesIO(obj.get()["Body"].read()) as f:
f.seek(0) # rewind the file
X, y = np.load(f).values()
s3fs = S3FileSystem()
with s3fs.open(f"s3://{bucket}/{key}") as s3file:
X, y = np.load(s3file).values()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With