Skip to content

Instantly share code, notes, and snippets.

@maedoc
Created September 24, 2024 13:47
Show Gist options
  • Save maedoc/31267ad8bca964083184cbb61600a60a to your computer and use it in GitHub Desktop.
Save maedoc/31267ad8bca964083184cbb61600a60a to your computer and use it in GitHub Desktop.
use minio s3 bucket easily from python
import os
import time
import s3fs
fs = s3fs.S3FileSystem(
endpoint_url="http://10.67.123.1:9000",
key="adni-user",
secret="12345679"
)
bucket = "adni3"
# find files easily with glob
subject_folders = fs.glob(f"{bucket}/adni3_pt/*_S_*")
print(len(subject_folders), 'subjects')
# open files directly in python
subject_file = 'adni3/adni3_pt/002_S_0413/T1w/002_S_0413/AV45.output/mri_gtmpvc.log'
with fs.open(subject_file) as fd:
for line in fd.readlines()[-10:]:
print('[mri_gtmpvc]', line.decode('ascii').strip())
# or download them for use with other tools
t2w = 'adni3/adni3_pt/002_S_0413/T2w'
tik = time.time()
fs.download(t2w, '/tmp/foo', recursive=True)
tok = time.time()
os.system('du -h -d1 /tmp/foo')
print(f'download took {tok-tik:0.1f} s')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment