Skip to content

Commit 0b1e3bc

Browse files
authored
Merge pull request #781 from guzman-raphael/filepath-delete
Fix S3 remove_object and add external location checks
2 parents 9e6cd0e + d4e4153 commit 0b1e3bc

16 files changed

+63
-42
lines changed

LNX-docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ services:
1919
- MINIO_ACCESS_KEY=datajoint
2020
- MINIO_SECRET_KEY=datajoint
2121
# ports:
22-
# - "9000:9000"
22+
# - "80:80"
2323
# volumes:
2424
# - ./minio/config:/root/.minio
2525
# - ./minio/data:/data
26-
command: server /data
26+
command: server --address ":80" /data
2727
healthcheck:
28-
test: ["CMD", "curl", "--fail", "http://minio:9000/minio/health/live"]
28+
test: ["CMD", "curl", "--fail", "http://minio:80/minio/health/live"]
2929
timeout: 5s
3030
retries: 60
3131
interval: 1s
3232
fakeservices.datajoint.io:
3333
<<: *net
34-
image: raphaelguzman/nginx:v0.0.4
34+
image: raphaelguzman/nginx:v0.0.5
3535
environment:
3636
- ADD_db_TYPE=DATABASE
3737
- ADD_db_ENDPOINT=db:3306
3838
- ADD_minio_TYPE=MINIO
39-
- ADD_minio_ENDPOINT=minio:9000
39+
- ADD_minio_ENDPOINT=minio:80
4040
- ADD_minio_PREFIX=/
4141
# ports:
42-
# - "9000:9000"
42+
# - "80:80"
4343
# - "443:443"
4444
# - "3306:3306"
4545
depends_on:
@@ -60,7 +60,7 @@ services:
6060
- DJ_TEST_HOST=fakeservices.datajoint.io
6161
- DJ_TEST_USER=datajoint
6262
- DJ_TEST_PASSWORD=datajoint
63-
- S3_ENDPOINT=fakeservices.datajoint.io:9000
63+
- S3_ENDPOINT=fakeservices.datajoint.io
6464
- S3_ACCESS_KEY=datajoint
6565
- S3_SECRET_KEY=datajoint
6666
- S3_BUCKET=datajoint-test

datajoint/errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class MissingExternalFile(DataJointError):
8181
"""
8282

8383

84+
class BucketInaccessible(DataJointError):
85+
"""
86+
Error raised when a S3 bucket is inaccessible
87+
"""
88+
89+
8490
# environment variables to control availability of experimental features
8591

8692
ADAPTED_TYPE_SWITCH = "DJ_SUPPORT_ADAPTED_TYPES"

datajoint/external.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def __init__(self, connection, store=None, database=None):
4646
if not self.is_declared:
4747
self.declare()
4848
self._s3 = None
49+
if self.spec['protocol'] == 'file' and not Path(self.spec['location']).is_dir():
50+
raise FileNotFoundError('Inaccessible local directory %s' %
51+
self.spec['location']) from None
4952

5053
@property
5154
def definition(self):

datajoint/s3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Folder:
1414
A Folder instance manipulates a flat folder of objects within an S3-compatible object store
1515
"""
1616
def __init__(self, endpoint, bucket, access_key, secret_key, *, secure=False, **_):
17-
self.client = minio.Minio(endpoint, access_key=access_key, secret_key=secret_key, secure=secure)
17+
self.client = minio.Minio(endpoint, access_key=access_key, secret_key=secret_key,
18+
secure=secure)
1819
self.bucket = bucket
1920
if not self.client.bucket_exists(bucket):
20-
warnings.warn('Creating bucket "%s"' % self.bucket)
21-
self.client.make_bucket(self.bucket)
21+
raise errors.BucketInaccessible('Inaccessible s3 bucket %s' % bucket) from None
2222

2323
def put(self, name, buffer):
2424
return self.client.put_object(
@@ -63,6 +63,6 @@ def get_size(self, name):
6363

6464
def remove_object(self, name):
6565
try:
66-
self.client.remove_objects(self.bucket, str(name))
66+
self.client.remove_object(self.bucket, str(name))
6767
except minio.ResponseError:
6868
return errors.DataJointError('Failed to delete %s from s3 storage' % name)

local-docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ services:
2020
- MINIO_ACCESS_KEY=datajoint
2121
- MINIO_SECRET_KEY=datajoint
2222
# ports:
23-
# - "9000:9000"
23+
# - "80:80"
2424
# To persist MinIO data and config
2525
# volumes:
2626
# - ./minio/data:/data
2727
# - ./minio/config:/root/.minio
28-
command: server /data
28+
command: server --address ":80" /data
2929
healthcheck:
30-
test: ["CMD", "curl", "--fail", "http://minio:9000/minio/health/live"]
30+
test: ["CMD", "curl", "--fail", "http://minio:80/minio/health/live"]
3131
timeout: 5s
3232
retries: 60
3333
interval: 1s
3434
fakeservices.datajoint.io:
3535
<<: *net
36-
image: raphaelguzman/nginx:v0.0.4
36+
image: raphaelguzman/nginx:v0.0.5
3737
environment:
3838
- ADD_db_TYPE=DATABASE
3939
- ADD_db_ENDPOINT=db:3306
4040
- ADD_minio_TYPE=MINIO
41-
- ADD_minio_ENDPOINT=minio:9000
41+
- ADD_minio_ENDPOINT=minio:80
4242
- ADD_minio_PREFIX=/
4343
ports:
44-
- "9000:9000"
44+
- "80:80"
4545
- "443:443"
4646
- "3306:3306"
4747
depends_on:
@@ -63,7 +63,7 @@ services:
6363
- DJ_TEST_USER=datajoint
6464
- DJ_TEST_PASSWORD=datajoint
6565
# If running tests locally, make sure to add entry in /etc/hosts for 127.0.0.1 fakeservices.datajoint.io
66-
- S3_ENDPOINT=fakeservices.datajoint.io:9000
66+
- S3_ENDPOINT=fakeservices.datajoint.io
6767
- S3_ACCESS_KEY=datajoint
6868
- S3_SECRET_KEY=datajoint
6969
- S3_BUCKET=datajoint-test

tests/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
secret_key=environ.get('S3_SECRET_KEY', 'datajoint'),
4242
bucket=environ.get('S3_BUCKET', 'datajoint-test'))
4343

44+
S3_MIGRATE_BUCKET = [path.stem for path in Path(
45+
Path(__file__).resolve().parent,
46+
'external-legacy-data', 's3').iterdir()][0]
47+
4448
# Prefix for all databases used during testing
4549
PREFIX = environ.get('DJ_TEST_DB_PREFIX', 'djtest')
4650
conn_root = dj.conn(**CONN_INFO_ROOT)
@@ -129,23 +133,22 @@ def setup_package():
129133
source = Path(
130134
Path(__file__).resolve().parent,
131135
'external-legacy-data','s3')
132-
bucket = "migrate-test"
133136
region = "us-east-1"
134137
try:
135-
minioClient.make_bucket(bucket, location=region)
138+
minioClient.make_bucket(S3_MIGRATE_BUCKET, location=region)
136139
except minio.error.BucketAlreadyOwnedByYou:
137140
pass
138141

139142
pathlist = Path(source).glob('**/*')
140143
for path in pathlist:
141144
if os.path.isfile(str(path)) and ".sql" not in str(path):
142145
minioClient.fput_object(
143-
bucket, str(Path(
144-
os.path.relpath(str(path),str(Path(source,bucket))))
146+
S3_MIGRATE_BUCKET, str(Path(
147+
os.path.relpath(str(path),str(Path(source,S3_MIGRATE_BUCKET))))
145148
.as_posix()), str(path))
146149
# Add S3
147150
try:
148-
minioClient.make_bucket("datajoint-test", location=region)
151+
minioClient.make_bucket(S3_CONN_INFO['bucket'], location=region)
149152
except minio.error.BucketAlreadyOwnedByYou:
150153
pass
151154

@@ -176,19 +179,17 @@ def teardown_package():
176179
remove("dj_local_conf.json")
177180

178181
# Remove old S3
179-
bucket = "migrate-test"
180182
objs = list(minioClient.list_objects_v2(
181-
bucket, recursive=True))
182-
objs = [minioClient.remove_object(bucket,
183+
S3_MIGRATE_BUCKET, recursive=True))
184+
objs = [minioClient.remove_object(S3_MIGRATE_BUCKET,
183185
o.object_name.encode('utf-8')) for o in objs]
184-
minioClient.remove_bucket(bucket)
186+
minioClient.remove_bucket(S3_MIGRATE_BUCKET)
185187

186188
# Remove S3
187-
bucket = "datajoint-test"
188-
objs = list(minioClient.list_objects_v2(bucket, recursive=True))
189-
objs = [minioClient.remove_object(bucket,
189+
objs = list(minioClient.list_objects_v2(S3_CONN_INFO['bucket'], recursive=True))
190+
objs = [minioClient.remove_object(S3_CONN_INFO['bucket'],
190191
o.object_name.encode('utf-8')) for o in objs]
191-
minioClient.remove_bucket(bucket)
192+
minioClient.remove_bucket(S3_CONN_INFO['bucket'])
192193

193194
# Remove old File Content
194195
shutil.rmtree(str(Path(os.path.expanduser('~'),'temp')))

tests/external-legacy-data/file/temp/migrate-test/djtest_blob_migrate/_Fhi2GUBB0fgxcSP2q-isgncIUTdgGK7ivHiySAU_94local renamed to tests/external-legacy-data/file/temp/datajoint-migrate/djtest_blob_migrate/_Fhi2GUBB0fgxcSP2q-isgncIUTdgGK7ivHiySAU_94local

File renamed without changes.

tests/external-legacy-data/file/temp/migrate-test/djtest_blob_migrate/e46pnXQW9GaCKbL3WxV1crGHeGqcE0OLInM_TTwAFfwlocal renamed to tests/external-legacy-data/file/temp/datajoint-migrate/djtest_blob_migrate/e46pnXQW9GaCKbL3WxV1crGHeGqcE0OLInM_TTwAFfwlocal

File renamed without changes.

tests/external-legacy-data/s3/migrate-test/maps/djtest_blob_migrate/FoRROa2LWM6_wx0RIQ0J-LVvgm256cqDQfJa066HoTEshared renamed to tests/external-legacy-data/s3/datajoint-migrate/maps/djtest_blob_migrate/FoRROa2LWM6_wx0RIQ0J-LVvgm256cqDQfJa066HoTEshared

File renamed without changes.

tests/external-legacy-data/s3/migrate-test/maps/djtest_blob_migrate/NmWj002gtKUkt9GIBwzn6Iw3x6h7ovlX_FfELbfjwRQshared renamed to tests/external-legacy-data/s3/datajoint-migrate/maps/djtest_blob_migrate/NmWj002gtKUkt9GIBwzn6Iw3x6h7ovlX_FfELbfjwRQshared

File renamed without changes.

0 commit comments

Comments
 (0)