From 2012500de8c482654f903087a142f5f56d4c7b84 Mon Sep 17 00:00:00 2001 From: lucio Date: Thu, 24 Nov 2022 14:29:41 +0100 Subject: [PATCH] added check on id exists --- .../impl/GCubeMongoStorageBackend.java | 20 +++++++++++++++---- .../storage/backend/impl/S3Backend.java | 20 ++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java index 69295f8..a894239 100644 --- a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java +++ b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java @@ -6,7 +6,9 @@ import java.util.Collections; import java.util.Map; import java.util.UUID; +import org.gcube.common.clients.exceptions.UnsupportedOperationException; import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.nodes.Content; import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; import org.gcube.common.storagehub.model.storages.MetaInfo; @@ -32,13 +34,16 @@ public class GCubeMongoStorageBackend extends StorageBackend { } @Override - public InputStream download(Content content) { + public InputStream download(Content content) throws StorageIdNotFoundException { return download(content.getStorageId()); } @Override - public InputStream download(String id) { - return getStorageClient(SecretManagerProvider.instance.get().getOwner().getId()).getClient().get().RFileAsInputStream(id); + public InputStream download(String id) throws StorageIdNotFoundException{ + IClient storageClient = getStorageClient(SecretManagerProvider.instance.get().getOwner().getId()).getClient(); + if (!storageClient.exist().RFile(id)) + throw new StorageIdNotFoundException(id, this.getPayloadConfiguration().getStorageName()); + return storageClient.get().RFileAsInputStream(id); } protected StorageClient getStorageClient(String login){ @@ -73,11 +78,18 @@ public class GCubeMongoStorageBackend extends StorageBackend { return info; } + @Override public MetaInfo upload(InputStream stream, String relPath, String name, Long size) { return this.upload(stream, relPath, name); } + + @Override + public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) { + throw new UnsupportedOperationException(); + } + @Override public void delete(String storageId) { log.debug("deleting object {} ",storageId); @@ -99,7 +111,7 @@ public class GCubeMongoStorageBackend extends StorageBackend { @Override public Map getFileMetadata(String id) { - return Collections.EMPTY_MAP; + return Collections.emptyMap(); } diff --git a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java index cdd3ecf..66bb7e9 100644 --- a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java +++ b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java @@ -1,11 +1,13 @@ package org.gcube.data.access.storagehub.storage.backend.impl; import java.io.InputStream; +import java.security.InvalidKeyException; import java.util.HashMap; import java.util.Map; import java.util.function.Function; import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.nodes.Content; import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; import org.gcube.common.storagehub.model.storages.MetaInfo; @@ -105,9 +107,14 @@ public class S3Backend extends StorageBackend{ @Override public MetaInfo upload(InputStream stream, String relativePath, String name, Long size) { - try { String storageId = keyGenerator.apply(null); + return upload(stream, relativePath, name, storageId, size); + } + @Override + public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) { + try { + Map headers = new HashMap<>(); headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY"); Map userMetadata = new HashMap<>(); @@ -136,20 +143,23 @@ public class S3Backend extends StorageBackend{ } } - + @Override - public InputStream download(String id) { + public InputStream download(String id) throws StorageIdNotFoundException{ try { InputStream inputStream = client.getObject(GetObjectArgs.builder().bucket(bucketName).object(id).build()); return inputStream; - } catch (Exception e) { + }catch (InvalidKeyException e) { + log.error("key %s not found", id); + throw new StorageIdNotFoundException(id, this.getPayloadConfiguration().getStorageName()); + }catch (Exception e) { log.error("error downloading file form s3"); throw new RuntimeException("error downloading file from s3",e); } } @Override - public InputStream download(Content content) { + public InputStream download(Content content) throws StorageIdNotFoundException { return download(content.getStorageId()); }