Search in sources :

Example 1 with ExternalStorageLocation

use of com.netflix.conductor.common.run.ExternalStorageLocation in project conductor by Netflix.

the class ClientBase method downloadFromExternalStorage.

/**
 * Uses the {@link PayloadStorage} for downloading large payloads to be used by the client.
 * Gets the uri of the payload fom the server and then downloads from this location.
 *
 * @param payloadType the {@link com.netflix.conductor.common.utils.ExternalPayloadStorage.PayloadType} to be downloaded
 * @param path        the relative of the payload in external storage
 * @return the payload object that is stored in external storage
 */
@SuppressWarnings("unchecked")
protected Map<String, Object> downloadFromExternalStorage(ExternalPayloadStorage.PayloadType payloadType, String path) {
    Preconditions.checkArgument(StringUtils.isNotBlank(path), "uri cannot be blank");
    ExternalStorageLocation externalStorageLocation = payloadStorage.getLocation(ExternalPayloadStorage.Operation.READ, payloadType, path);
    try (InputStream inputStream = payloadStorage.download(externalStorageLocation.getUri())) {
        return objectMapper.readValue(inputStream, Map.class);
    } catch (IOException e) {
        String errorMsg = String.format("Unable to download payload from external storage location: %s", path);
        logger.error(errorMsg, e);
        throw new ConductorClientException(errorMsg, e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ConductorClientException(com.netflix.conductor.client.exceptions.ConductorClientException) ExternalStorageLocation(com.netflix.conductor.common.run.ExternalStorageLocation)

Example 2 with ExternalStorageLocation

use of com.netflix.conductor.common.run.ExternalStorageLocation in project conductor by Netflix.

the class AzureBlobPayloadStorage method getLocation.

/**
 * @param operation   the type of {@link Operation} to be performed
 * @param payloadType the {@link PayloadType} that is being accessed
 * @return a {@link ExternalStorageLocation} object which contains the pre-signed URL and the azure blob name
 *  for the json payload
 */
@Override
public ExternalStorageLocation getLocation(Operation operation, PayloadType payloadType, String path) {
    try {
        ExternalStorageLocation externalStorageLocation = new ExternalStorageLocation();
        String objectKey;
        if (StringUtils.isNotBlank(path)) {
            objectKey = path;
        } else {
            objectKey = getObjectKey(payloadType);
        }
        externalStorageLocation.setPath(objectKey);
        BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(objectKey).getBlockBlobClient();
        String blobUrl = Utility.urlDecode(blockBlobClient.getBlobUrl());
        if (sasTokenCredential != null) {
            blobUrl = blobUrl + "?" + sasTokenCredential.getSasToken();
        } else {
            BlobSasPermission blobSASPermission = new BlobSasPermission();
            if (operation.equals(Operation.READ)) {
                blobSASPermission.setReadPermission(true);
            } else if (operation.equals(Operation.WRITE)) {
                blobSASPermission.setWritePermission(true);
                blobSASPermission.setCreatePermission(true);
            }
            BlobServiceSasSignatureValues blobServiceSasSignatureValues = new BlobServiceSasSignatureValues(OffsetDateTime.now(ZoneOffset.UTC).plusSeconds(expirationSec), blobSASPermission);
            blobUrl = blobUrl + "?" + blockBlobClient.generateSas(blobServiceSasSignatureValues);
        }
        externalStorageLocation.setUri(blobUrl);
        return externalStorageLocation;
    } catch (BlobStorageException e) {
        String msg = "Error communicating with Azure";
        logger.error(msg, e);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, msg, e);
    }
}
Also used : BlobServiceSasSignatureValues(com.azure.storage.blob.sas.BlobServiceSasSignatureValues) BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) BlobSasPermission(com.azure.storage.blob.sas.BlobSasPermission) ExternalStorageLocation(com.netflix.conductor.common.run.ExternalStorageLocation) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Example 3 with ExternalStorageLocation

use of com.netflix.conductor.common.run.ExternalStorageLocation in project conductor by Netflix.

the class AzureBlobPayloadStorageTest method testGetLocation.

private void testGetLocation(AzureBlobPayloadStorage azureBlobPayloadStorage, ExternalPayloadStorage.Operation operation, ExternalPayloadStorage.PayloadType payloadType, String expectedPath) {
    ExternalStorageLocation externalStorageLocation = azureBlobPayloadStorage.getLocation(operation, payloadType, null);
    assertNotNull(externalStorageLocation);
    assertNotNull(externalStorageLocation.getPath());
    assertTrue(externalStorageLocation.getPath().startsWith(expectedPath));
    assertNotNull(externalStorageLocation.getUri());
    assertTrue(externalStorageLocation.getUri().contains(expectedPath));
}
Also used : ExternalStorageLocation(com.netflix.conductor.common.run.ExternalStorageLocation)

Example 4 with ExternalStorageLocation

use of com.netflix.conductor.common.run.ExternalStorageLocation in project conductor by Netflix.

the class ExternalPayloadStorageUtils method uploadHelper.

@VisibleForTesting
String uploadHelper(byte[] payloadBytes, long payloadSize, ExternalPayloadStorage.PayloadType payloadType) {
    ExternalStorageLocation location = externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, payloadType, "");
    externalPayloadStorage.upload(location.getPath(), new ByteArrayInputStream(payloadBytes), payloadSize);
    return location.getPath();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExternalStorageLocation(com.netflix.conductor.common.run.ExternalStorageLocation) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with ExternalStorageLocation

use of com.netflix.conductor.common.run.ExternalStorageLocation in project conductor by Netflix.

the class S3PayloadStorage method getLocation.

/**
 * @param operation   the type of {@link Operation} to be performed
 * @param payloadType the {@link PayloadType} that is being accessed
 * @return a {@link ExternalStorageLocation} object which contains the pre-signed URL and the s3 object key for the json payload
 */
@Override
public ExternalStorageLocation getLocation(Operation operation, PayloadType payloadType, String path) {
    try {
        ExternalStorageLocation externalStorageLocation = new ExternalStorageLocation();
        Date expiration = new Date();
        long expTimeMillis = expiration.getTime() + 1000 * expirationSec;
        expiration.setTime(expTimeMillis);
        HttpMethod httpMethod = HttpMethod.GET;
        if (operation == Operation.WRITE) {
            httpMethod = HttpMethod.PUT;
        }
        String objectKey;
        if (StringUtils.isNotBlank(path)) {
            objectKey = path;
        } else {
            objectKey = getObjectKey(payloadType);
        }
        externalStorageLocation.setPath(objectKey);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey).withMethod(httpMethod).withExpiration(expiration);
        externalStorageLocation.setUri(s3Client.generatePresignedUrl(generatePresignedUrlRequest).toURI().toASCIIString());
        return externalStorageLocation;
    } catch (SdkClientException e) {
        String msg = "Error communicating with S3";
        logger.error(msg, e);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, msg, e);
    } catch (URISyntaxException e) {
        String msg = "Invalid URI Syntax";
        logger.error(msg, e);
        throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, msg, e);
    }
}
Also used : GeneratePresignedUrlRequest(com.amazonaws.services.s3.model.GeneratePresignedUrlRequest) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SdkClientException(com.amazonaws.SdkClientException) URISyntaxException(java.net.URISyntaxException) ExternalStorageLocation(com.netflix.conductor.common.run.ExternalStorageLocation) Date(java.util.Date) HttpMethod(com.amazonaws.HttpMethod)

Aggregations

ExternalStorageLocation (com.netflix.conductor.common.run.ExternalStorageLocation)10 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)2 Test (org.junit.Test)2 HttpMethod (com.amazonaws.HttpMethod)1 SdkClientException (com.amazonaws.SdkClientException)1 GeneratePresignedUrlRequest (com.amazonaws.services.s3.model.GeneratePresignedUrlRequest)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 BlobSasPermission (com.azure.storage.blob.sas.BlobSasPermission)1 BlobServiceSasSignatureValues (com.azure.storage.blob.sas.BlobServiceSasSignatureValues)1 BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ConductorClientException (com.netflix.conductor.client.exceptions.ConductorClientException)1 ExternalPayloadStorage (com.netflix.conductor.common.utils.ExternalPayloadStorage)1 JsonMapperProvider (com.netflix.conductor.common.utils.JsonMapperProvider)1 Configuration (com.netflix.conductor.core.config.Configuration)1 TestConfiguration (com.netflix.conductor.core.execution.TestConfiguration)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1