package com.nds; import com.azure.storage.blob.BlobClient; import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.BlobServiceClientBuilder; import com.azure.storage.blob.sas.BlobSasPermission; import com.azure.storage.blob.sas.BlobServiceSasSignatureValues; import com.azure.storage.common.StorageSharedKeyCredential; import com.google.inject.Inject; import com.snaplogic.account.api.capabilities.Accounts; import com.snaplogic.api.ConfigurationException; import com.snaplogic.common.SnapType; import com.snaplogic.common.properties.SnapProperty; import com.snaplogic.common.properties.builders.PropertyBuilder; import com.snaplogic.snap.api.*; import com.snaplogic.snap.api.capabilities.*; import java.time.OffsetDateTime; import java.util.LinkedHashMap; import java.util.Map; @General(title = "NewBlob", purpose = "Generate SaS for Blob", author = "NDDS", docLink = "http://www.snaplogic.com") @Inputs(min = 0, max = 1, accepts = {ViewType.DOCUMENT}) @Outputs(min = 1, max = 1, offers = {ViewType.BINARY}) @Version(snap = 1) @Category(snap = SnapCategory.) @Accounts(provides = {Custom.class}, optional = false) public class NewBlob extends SimpleSnap { private static final String ACCOUNT_NAME_COPY = "accountNameCopy"; private String accountNameCopy; private static final String CONNECTION_STRING_COPY = "connectionStringCopy"; private String connectionStringCopy; private static final String BLOB_CONTAINER = "blobContainer"; private String blobContainer; private static final String BLOB_NAME = "blobName"; private String blobName; @Inject private Custom snapAccount; @Override public void defineProperties(PropertyBuilder propertyBuilder) { propertyBuilder.describe(ACCOUNT_NAME_COPY, "Account Name", "Account Name of Blob") .required() .expression(SnapProperty.DecoratorType.ENABLED_EXPRESSION) .defaultValue("account.accountName") .add(); propertyBuilder.describe(CONNECTION_STRING_COPY, "Connection String", "Connection String of Blob") .required() .expression(SnapProperty.DecoratorType.ENABLED_EXPRESSION) .defaultValue("account.connectionString") .add(); propertyBuilder.describe(BLOB_CONTAINER, "BlobContainer", "Blob Container Name") .required() .type(SnapType.ANY) .expression() .schemaAware(SnapProperty.DecoratorType.ACCEPTS_SCHEMA) .defaultValue("claire-eudb-adj").add(); propertyBuilder.describe(BLOB_NAME, "Blob Name", "Enter the Blob Name") .required() .type(SnapType.ANY) .expression() .schemaAware(SnapProperty.DecoratorType.ACCEPTS_SCHEMA) .defaultValue("devTest/SFTP2Blob.csv") .add(); } @Override public void configure(PropertyValues propertyValues) throws ConfigurationException { accountNameCopy = propertyValues.getAsExpression(ACCOUNT_NAME_COPY).eval(null); connectionStringCopy = propertyValues.getAsExpression(CONNECTION_STRING_COPY).eval(null); blobContainer = propertyValues.get(BLOB_CONTAINER); blobName = propertyValues.get(BLOB_NAME); } @Override protected void process(Document document, String s) { Map data = new LinkedHashMap<>(); StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountNameCopy, connectionStringCopy); BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() .endpoint("https://" + accountNameCopy + ".blob.core.windows.net/") .credential(credential) .buildClient(); data.put("Connection", "Established"); BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(blobContainer); BlobClient blobClient = containerClient.getBlobClient(blobName); // SAS_properties OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1); BlobSasPermission permission = new BlobSasPermission().setReadPermission(true); BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission) .setStartTime(OffsetDateTime.now()); String sasToken = blobClient.generateSas(values); String blobSaS = "https://" + accountNameCopy + ".blob.core.windows.net/" + blobContainer +"/"+ blobName + "?" + sasToken; data.put("sasToken", sasToken); data.put("blobSaS", blobSaS); outputViews.write(documentUtility.newDocumentFor(document, data)); } }