added check on id exists

This commit is contained in:
Lucio Lelii 2022-11-24 14:29:41 +01:00
parent 8b235da142
commit 2012500de8
2 changed files with 31 additions and 9 deletions

View File

@ -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<String, String> getFileMetadata(String id) {
return Collections.EMPTY_MAP;
return Collections.emptyMap();
}

View File

@ -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,8 +107,13 @@ 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<String, String> headers = new HashMap<>();
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
@ -138,18 +145,21 @@ 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());
}