com.amazonaws.util.IOUtils Java Examples

The following examples show how to use com.amazonaws.util.IOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RegistryBackedProfileFactory.java    From halyard with Apache License 2.0 6 votes vote down vote up
@Override
protected Profile getBaseProfile(String name, String version, String outputFile) {
  try {
    return new Profile(
        name,
        version,
        outputFile,
        IOUtils.toString(profileRegistry.readProfile(getArtifact().getName(), version, name)));
  } catch (RetrofitError | IOException e) {
    throw new HalException(
        new ConfigProblemBuilder(
                FATAL, "Unable to retrieve profile \"" + name + "\": " + e.getMessage())
            .build(),
        e);
  }
}
 
Example #2
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
public String upload(InputStream is, String fileKey, String fileName, String suffix, Boolean isPublic) throws Exception {
    validateFile(is, suffix);
    if (isPublic == null) {
        isPublic = Boolean.TRUE;
    }
    if ((is != null) && (fileKey != null)) {
        try {
            byte[] bytes = IOUtils.toByteArray(is);
            s3Client.putObject(
                    new PutObjectRequest(
                    		s3BucketConfig.getName(),
                            fileKey,
                            new ByteArrayInputStream(bytes),
                            S3ObjectMetadata.getObjectMetadata(bytes)
                    ).withCannedAcl(isPublic ? CannedAccessControlList.PublicRead : CannedAccessControlList.AuthenticatedRead)
            );
            return fileName + '.' + suffix;
        } catch (AmazonServiceException | IOException e) {
            throw new BusinessException(Validations.INVALID_S3_BUCKET_CREDENTIALS.getCode());
        } finally {
            is.close();
        }
    } else {
        throw new BusinessException(Validations.INVALID_PARAMETERS.getCode());
    }
}
 
Example #3
Source File: S3S3CopierTest.java    From circus-train with Apache License 2.0 6 votes vote down vote up
@Test
public void copyOneFile() throws Exception {
  client.putObject("source", "data", inputData);

  Path sourceBaseLocation = new Path("s3://source/data");
  Path replicaLocation = new Path("s3://target/data2");
  List<Path> sourceSubLocations = new ArrayList<>();
  S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
  Metrics metrics = s3s3Copier.copy();
  assertThat(metrics.getBytesReplicated(), is(7L));
  assertThat(metrics.getMetrics().get(S3S3CopierMetrics.Metrics.TOTAL_BYTES_TO_REPLICATE.name()), is(7L));
  S3Object object = client.getObject("target", "data2");
  String data = IOUtils.toString(object.getObjectContent());
  assertThat(data, is("bar foo"));
  assertThat(registry.getGauges().containsKey(RunningMetrics.S3S3_CP_BYTES_REPLICATED.name()), is(true));
}
 
Example #4
Source File: S3S3CopierTest.java    From circus-train with Apache License 2.0 6 votes vote down vote up
@Test
public void copyOneObject() throws Exception {
  client.putObject("source", "data", inputData);

  Path sourceBaseLocation = new Path("s3://source/");
  Path replicaLocation = new Path("s3://target/");
  List<Path> sourceSubLocations = new ArrayList<>();
  S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
  Metrics metrics = s3s3Copier.copy();
  assertThat(metrics.getBytesReplicated(), is(7L));
  assertThat(metrics.getMetrics().get(S3S3CopierMetrics.Metrics.TOTAL_BYTES_TO_REPLICATE.name()), is(7L));
  S3Object object = client.getObject("target", "data");
  String data = IOUtils.toString(object.getObjectContent());
  assertThat(data, is("bar foo"));
  assertThat(registry.getGauges().containsKey(RunningMetrics.S3S3_CP_BYTES_REPLICATED.name()), is(true));
}
 
Example #5
Source File: AmazonS3OAuthStateService.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Override
public boolean isAvailableInDatabase(String state) {
    AmazonS3 s3 = this.createS3Client();
    S3Object s3Object = getObject(s3, getKey(state));
    if (s3Object == null) {
        return false;
    }
    String millisToExpire = null;
    try {
        millisToExpire = IOUtils.toString(s3Object.getObjectContent());
        return Long.valueOf(millisToExpire) > System.currentTimeMillis();
    } catch (IOException e) {
        log.error("Failed to load a state data for state: {}", state, e);
        return false;
    } catch (NumberFormatException ne) {
        log.error("Invalid state value detected - state: {}, millisToExpire: {}", state, millisToExpire);
        return false;
    }
}
 
Example #6
Source File: InfectedFileCache.java    From aws-s3-virusscan with Apache License 2.0 6 votes vote down vote up
public List<InfectedFile> getFiles() {
    final List<InfectedFile> files = new ArrayList<>();
    if (Config.has(Config.Key.INFECTED_FILES_BUCKET_NAME)) {
        final AmazonS3 s3local = AmazonS3ClientBuilder.standard().withCredentials(this.credentialsProvider).withRegion(Config.get(Config.Key.INFECTED_FILES_BUCKET_REGION)).build();
        ObjectListing objectListing = s3local.listObjects(Config.get(Config.Key.INFECTED_FILES_BUCKET_NAME));
        while (true) {
            objectListing.getObjectSummaries().forEach((summary) -> {
                final S3Object object = s3local.getObject(summary.getBucketName(), summary.getKey());
                final byte[] content;
                try {
                    content = IOUtils.toByteArray(object.getObjectContent());
                } catch (final IOException e) {
                    throw new RuntimeException(e);
                }
                files.add(new InfectedFile(summary.getKey(), content, object.getObjectMetadata().getContentType()));
            });
            if (objectListing.isTruncated()) {
                objectListing = s3local.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
    }
    return files;
}
 
Example #7
Source File: MainActivity.java    From aws-iotbot with Apache License 2.0 6 votes vote down vote up
public void GetFromS3(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                S3ObjectInputStream content = s3Client.getObject(BUCKET, KEY).getObjectContent();
                byte[] bytes = IOUtils.toByteArray(content);
                bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                setBitmap(bitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageFromS3.setScaleType(ImageView.ScaleType.FIT_XY);
                        imageFromS3.setImageBitmap(getBitmap());
                    }
                });
            } catch (Exception e) {
                Log.e(LOG_TAG,
                        "Exception occurred when retrieving image from S3",
                        e);
            }
        }
    }).start();
}
 
Example #8
Source File: S3S3CopierTest.java    From circus-train with Apache License 2.0 6 votes vote down vote up
@Test
public void copyMultipleObjects() throws Exception {
  // Making sure we only request 1 file at the time so we need to loop
  ListObjectsRequestFactory mockListObjectRequestFactory = Mockito.mock(ListObjectsRequestFactory.class);
  when(mockListObjectRequestFactory.newInstance()).thenReturn(new ListObjectsRequest().withMaxKeys(1));

  client.putObject("source", "bar/data1", inputData);
  client.putObject("source", "bar/data2", inputData);

  Path sourceBaseLocation = new Path("s3://source/bar/");
  Path replicaLocation = new Path("s3://target/foo/");
  List<Path> sourceSubLocations = new ArrayList<>();
  S3S3Copier s3s3Copier = new S3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation, s3ClientFactory,
      transferManagerFactory, mockListObjectRequestFactory, registry, s3S3CopierOptions);
  Metrics metrics = s3s3Copier.copy();
  assertThat(metrics.getBytesReplicated(), is(14L));

  S3Object object1 = client.getObject("target", "foo/data1");
  String data1 = IOUtils.toString(object1.getObjectContent());
  assertThat(data1, is("bar foo"));
  S3Object object2 = client.getObject("target", "foo/data2");
  String data2 = IOUtils.toString(object2.getObjectContent());
  assertThat(data2, is("bar foo"));
}
 
Example #9
Source File: LambdaTest.java    From github-bucket with ISC License 6 votes vote down vote up
private SNSEvent createSnsEvent(final String githubEvent) {
    SNSEvent.SNS sns = new SNSEvent.SNS();
    sns.setMessageAttributes(new HashMap<String, SNSEvent.MessageAttribute>(1, 1) {
        {
            SNSEvent.MessageAttribute attr = new SNSEvent.MessageAttribute();
            attr.setValue(githubEvent);
            put("X-Github-Event", attr);
        }
    });
    try (InputStream is = getClass().getResourceAsStream("/github-push-payload.json")) {
        sns.setMessage(IOUtils.toString(is));
    }
    catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

    SNSEvent.SNSRecord record = new SNSEvent.SNSRecord();
    record.setSns(sns);

    SNSEvent snsEvent = new SNSEvent();
    snsEvent.setRecords(Collections.singletonList(record));
    return snsEvent;
}
 
Example #10
Source File: ExecutionDAOFacadeTest.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Test
public void tesGetWorkflowById() throws Exception {
    when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(new Workflow());
    Workflow workflow = executionDAOFacade.getWorkflowById("workflowId", true);
    assertNotNull(workflow);
    verify(indexDAO, never()).get(any(), any());

    when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(null);
    InputStream stream = ExecutionDAOFacadeTest.class.getResourceAsStream("/test.json");
    byte[] bytes = IOUtils.toByteArray(stream);
    String jsonString = new String(bytes);
    when(indexDAO.get(any(), any())).thenReturn(jsonString);
    workflow = executionDAOFacade.getWorkflowById("workflowId", true);
    assertNotNull(workflow);
    verify(indexDAO, times(1)).get(any(), any());
}
 
Example #11
Source File: S3Service.java    From fullstop with Apache License 2.0 6 votes vote down vote up
public String downloadObject(final String bucketName, final String key) {

        final S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));

        final InputStream inputStream = object.getObjectContent();

        String result = null;
        try {
            result = IOUtils.toString(inputStream);
        } catch (final IOException e) {
            log.warn("Could not download file for bucket: {}, with key: {}", bucketName, key);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (final IOException ex) {
                    log.debug("Ignore failure in closing the Closeable", ex);
                }
            }
        }

        log.info("Downloaded file for bucket: {}, with key: {}", bucketName, key);
        return result;
    }
 
Example #12
Source File: ArtifactService.java    From halyard with Apache License 2.0 6 votes vote down vote up
public void writeBom(String bomPath) {
  if (googleWriteableProfileRegistry == null) {
    throw new HalException(FATAL, NO_WRITER_ENABLED);
  }

  BillOfMaterials bom;
  String bomContents;
  String version;

  try {
    bomContents = IOUtils.toString(new FileInputStream(bomPath));
    bom = relaxedObjectMapper.convertValue(yamlParser.load(bomContents), BillOfMaterials.class);
    version = bom.getVersion();
  } catch (IOException e) {
    throw new HalException(
        new ConfigProblemBuilder(FATAL, "Unable to load Bill of Materials: " + e.getMessage())
            .build());
  }

  if (version == null) {
    throw new HalException(
        new ConfigProblemBuilder(FATAL, "No version was supplied in this BOM.").build());
  }

  googleWriteableProfileRegistry.writeBom(bom.getVersion(), bomContents);
}
 
Example #13
Source File: LambdaTest.java    From github-bucket with ISC License 6 votes vote down vote up
private SNSEvent createSnsEvent(final String githubEvent) {
    SNSEvent.SNS sns = new SNSEvent.SNS();
    sns.setMessageAttributes(new HashMap<String, SNSEvent.MessageAttribute>(1, 1) {
        {
            SNSEvent.MessageAttribute attr = new SNSEvent.MessageAttribute();
            attr.setValue(githubEvent);
            put("X-Github-Event", attr);
        }
    });
    try (InputStream is = getClass().getResourceAsStream("/github-push-payload.json")) {
        sns.setMessage(IOUtils.toString(is));
    }
    catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

    SNSEvent.SNSRecord record = new SNSEvent.SNSRecord();
    record.setSns(sns);

    SNSEvent snsEvent = new SNSEvent();
    snsEvent.setRecords(Collections.singletonList(record));
    return snsEvent;
}
 
Example #14
Source File: EscrowedEncryptExample.java    From aws-encryption-sdk-java with Apache License 2.0 6 votes vote down vote up
private static void escrowDecrypt(final String fileName) throws Exception {
    // You can decrypt the stream using only the private key.
    // This method does not call AWS KMS.

    // 1. Instantiate the SDK
    final AwsCrypto crypto = new AwsCrypto();

    // 2. Instantiate a JCE master key provider
    // This method call uses the escrowed private key, not null 
    final JceMasterKey escrowPriv = JceMasterKey.getInstance(publicEscrowKey, privateEscrowKey, "Escrow", "Escrow",
            "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");

    // 3. Decrypt the file
    // To simplify the code, we omit the encryption context. Production code should always 
    // use an encryption context. For an example, see the other SDK samples.
    final FileInputStream in = new FileInputStream(fileName + ".encrypted");
    final FileOutputStream out = new FileOutputStream(fileName + ".deescrowed");
    final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(escrowPriv, out);
    IOUtils.copy(in, decryptingStream);
    in.close();
    decryptingStream.close();

}
 
Example #15
Source File: SecretModel.java    From strongbox with Apache License 2.0 6 votes vote down vote up
private byte[] fromStdin() {
    try {

        InputStream inputStream = System.in;
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
        if (!inputReader.ready()) {
            // Interactive
            char[] secretValue = System.console().readPassword("Enter secret value:");

            if (secretValue == null) {
                throw new IllegalArgumentException("A secret value must be specified");
            }
            return SecretValueConverter.asBytes(secretValue);
        } else {
            // Piped in
            return IOUtils.toByteArray(inputStream);
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to read secret value from stdin", e);
    }
}
 
Example #16
Source File: RepositoryS3.java    From github-bucket with ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example #17
Source File: SecureShellAuthentication.java    From github-bucket with ISC License 5 votes vote down vote up
public SecureShellAuthentication(Bucket bucket, AmazonS3 client) {
    factory = new JschConfigSessionFactory() {

        @Override
        public synchronized RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
            // Do not check for default ssh user config
            fs.setUserHome(null);
            return super.getSession(uri, credentialsProvider, fs, tms);
        }

        @Override
        protected void configure(OpenSshConfig.Host host, Session session) {
            session.setConfig("HashKnownHosts", "no");
            if ("localhost".equalsIgnoreCase(host.getHostName())) {
                session.setConfig("StrictHostKeyChecking", "no");
            }
        }

        @Override
        protected void configureJSch(JSch jsch) {
            S3Object file;
            file = client.getObject(bucket.getName(), ".ssh/known_hosts");
            try (InputStream is = file.getObjectContent()) {
                jsch.setKnownHosts(is);
            } catch (IOException | JSchException e) {
                throw new IllegalArgumentException("Missing known hosts file on s3: .ssh/known_hosts", e);
            }
            file = client.getObject(bucket.getName(), ".ssh/id_rsa");
            try (InputStream is = file.getObjectContent()) {
                jsch.addIdentity("git", IOUtils.toByteArray(is), null, new byte[0]);
            } catch (IOException | JSchException e) {
                throw new IllegalArgumentException("Missing key file on s3: .ssh/id_rsa", e);
            }
        }
    };
}
 
Example #18
Source File: AmazonS3InstallationService.java    From java-slack-sdk with MIT License 5 votes vote down vote up
private Installer toInstaller(S3Object s3Object) throws IOException {
    if (s3Object == null) {
        return null;
    }
    String json = IOUtils.toString(s3Object.getObjectContent());
    return JsonOps.fromJson(json, DefaultInstaller.class);
}
 
Example #19
Source File: RepositoryS3.java    From github-bucket with ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example #20
Source File: EscrowedEncryptExample.java    From aws-encryption-sdk-java with Apache License 2.0 5 votes vote down vote up
private static void standardEncrypt(final String kmsArn, final String fileName) throws Exception {
    // Encrypt with the KMS CMK and the escrowed public key
    // 1. Instantiate the SDK
    final AwsCrypto crypto = new AwsCrypto();

    // 2. Instantiate a KMS master key provider
    final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
    
    // 3. Instantiate a JCE master key provider
    // Because the user does not have access to the private escrow key,
    // they pass in "null" for the private key parameter.
    final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
            "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");

    // 4. Combine the providers into a single master key provider
    final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub);

    // 5. Encrypt the file
    // To simplify the code, we omit the encryption context. Production code should always 
    // use an encryption context. For an example, see the other SDK samples.
    final FileInputStream in = new FileInputStream(fileName);
    final FileOutputStream out = new FileOutputStream(fileName + ".encrypted");
    final CryptoOutputStream<?> encryptingStream = crypto.createEncryptingStream(provider, out);

    IOUtils.copy(in, encryptingStream);
    in.close();
    encryptingStream.close();
}
 
Example #21
Source File: AmazonS3InstallationService.java    From java-slack-sdk with MIT License 5 votes vote down vote up
private Bot toBot(S3Object s3Object) throws IOException {
    if (s3Object == null) {
        return null;
    }
    String json = IOUtils.toString(s3Object.getObjectContent());
    return JsonOps.fromJson(json, DefaultBot.class);
}
 
Example #22
Source File: EscrowedEncryptExample.java    From aws-encryption-sdk-java with Apache License 2.0 5 votes vote down vote up
private static void standardDecrypt(final String kmsArn, final String fileName) throws Exception {
    // Decrypt with the KMS CMK and the escrow public key. You can use a combined provider,
    // as shown here, or just the KMS master key provider.

    // 1. Instantiate the SDK
    final AwsCrypto crypto = new AwsCrypto();

    // 2. Instantiate a KMS master key provider
    final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
    
    // 3. Instantiate a JCE master key provider
    // Because the user does not have access to the private 
    // escrow key, they pass in "null" for the private key parameter.
    final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
            "RSA/ECB/OAEPWithSHA-512AndMGF1Padding");

    // 4. Combine the providers into a single master key provider
    final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub);

    // 5. Decrypt the file
    // To simplify the code, we omit the encryption context. Production code should always 
    // use an encryption context. For an example, see the other SDK samples.
    final FileInputStream in = new FileInputStream(fileName + ".encrypted");
    final FileOutputStream out = new FileOutputStream(fileName + ".decrypted");
    final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, out);
    IOUtils.copy(in, decryptingStream);
    in.close();
    decryptingStream.close();
}
 
Example #23
Source File: VaultService.java    From halyard with Apache License 2.0 5 votes vote down vote up
public void publishSecret(
    DeploymentConfiguration deploymentConfiguration, String name, Path path) {
  String contents;
  try {
    contents = IOUtils.toString(new FileInputStream(path.toFile()));
  } catch (IOException e) {
    throw new HalException(
        Problem.Severity.FATAL,
        "Failed to read config file " + path.toString() + ": " + e.getMessage());
  }

  publishSecret(deploymentConfiguration, name, contents);
}
 
Example #24
Source File: FileService.java    From halyard with Apache License 2.0 5 votes vote down vote up
private byte[] readFromLocalFilesystem(String path) throws IOException {
  Path absolutePath = absolutePath(path);
  if (absolutePath == null) {
    throw new IOException(
        "Provided path: \"" + path + "\" cannot be resolved to a local absolute path.");
  }
  return IOUtils.toByteArray(new FileInputStream(absolutePath.toString()));
}
 
Example #25
Source File: Lambda.java    From alexa-skills-kit-tester-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final InputStream input, final OutputStream output, final Context context) throws IOException {
    final String inputS = IOUtils.toString(Optional.ofNullable(input).orElse(new ByteArrayInputStream("{}".getBytes())));
    final JsonNode root = om.readTree(inputS);

    final String bucket = Optional.ofNullable(root.get(S3_BUCKET_PROPERTY)).map(JsonNode::textValue).filter(StringUtils::isNotBlank).orElse(System.getenv(S3_BUCKET_PROPERTY));
    Validate.notBlank(bucket, S3_BUCKET_PROPERTY + " hasn't been set in the request payload nor as an environment variable.");

    final String key = Optional.ofNullable(root.get(S3_KEY_PROPERTY)).map(JsonNode::textValue).filter(StringUtils::isNotBlank)
            .orElse(System.getenv(S3_KEY_PROPERTY));
    final String region = Optional.ofNullable(root.get(S3_REGION_PROPERTY)).map(JsonNode::textValue).filter(StringUtils::isNotBlank)
            .orElse(System.getenv(S3_REGION_PROPERTY));

    final AmazonS3 s3client = StringUtils.isNotBlank(region) ? AmazonS3ClientBuilder.standard().withRegion(region).build() : AmazonS3ClientBuilder.defaultClient();

    final ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(Optional.ofNullable(key).map(k -> k + (k.endsWith("/") ? "" : "/")).orElse(""));

    log.info("[INFO] Reading out *.yml conversation script files in folder '" + listRequest.getPrefix() + "' in bucket '" + listRequest.getBucketName() + "'");

    final List<S3ObjectSummary> conversationScripts = s3client.listObjects(listRequest).getObjectSummaries().stream()
            .filter(os -> os.getKey().toLowerCase().endsWith(".yml")).collect(Collectors.toList());

    log.info("[INFO] Found " + conversationScripts.size() + " conversation script files in bucket '" + bucket + "'");

    for (final S3ObjectSummary conversationScript : conversationScripts) {
        log.info("[INFO] Load conversation script file " + conversationScript.getKey() + " from S3 bucket " + bucket);

        AlexaClient.create(s3client.getObject(bucket, conversationScript.getKey()).getObjectContent())
                .build()
                .startScript();
    }
    output.write("{ \"OK\" }".getBytes());
}
 
Example #26
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
public String upload(InputStream is, String fileName, String suffix, Boolean isPublic) throws Exception {
    validateFile(is, suffix);
    if (isPublic == null) {
        isPublic = Boolean.TRUE;
    }
    if ((is != null) && (fileName != null)) {

        try {
            byte[] bytes = IOUtils.toByteArray(is);
            s3Client.putObject(
                    new PutObjectRequest(
                    		cdnConfig.getName(),
                            fileName + '.' + suffix,
                            new ByteArrayInputStream(bytes),
                            S3ObjectMetadata.getObjectMetadata(bytes)
                    ).withCannedAcl(isPublic ? CannedAccessControlList.PublicRead : CannedAccessControlList.AuthenticatedRead)
            );
            return fileName + '.' + suffix;
        } catch (AmazonServiceException | IOException e) {
            throw new BusinessException(Validations.INVALID_S3_BUCKET_CREDENTIALS.getCode());
        } finally {
            is.close();
        }
    } else {
        throw new BusinessException(Validations.INVALID_PARAMETERS.getCode());
    }
}
 
Example #27
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Override
public String downloadAsBase64(String filePath) throws Exception {
    if (!Optional.ofNullable(filePath).isPresent()) {
        throw new Exception(UploadService.Validations.INVALID_PATH.getCode());
    }
    try {
        return Base64.encodeAsString(IOUtils.toByteArray(downloadFile(filePath)));
    } catch (IOException e) {
        throw new Exception(e.getMessage());
    }
}
 
Example #28
Source File: S3.java    From lambadaframework with MIT License 5 votes vote down vote up
/**
 * Reads the file from S3 bucket and returns as a string.
 *
 * @return File content
 * @throws IOException
 */
public static String getFile(String bucket, String key) throws IOException {
    S3Object object = getS3Client().getObject(
            new GetObjectRequest(bucket, key));
    InputStream objectData = object.getObjectContent();
    String theString = IOUtils.toString(objectData);
    objectData.close();
    return theString;
}
 
Example #29
Source File: ExternalPayloadStorageUtils.java    From conductor with Apache License 2.0 5 votes vote down vote up
/**
 * Download the payload from the given path
 *
 * @param path the relative path of the payload in the {@link ExternalPayloadStorage}
 * @return the payload object
 * @throws ApplicationException in case of JSON parsing errors or download errors
 */
@SuppressWarnings("unchecked")
public Map<String, Object> downloadPayload(String path) {
    try (InputStream inputStream = externalPayloadStorage.download(path)) {
        return objectMapper.readValue(IOUtils.toString(inputStream), Map.class);
    } catch (IOException e) {
        logger.error("Unable to download payload from external storage path: {}", path, e);
        throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, e);
    }
}
 
Example #30
Source File: PSQLXyzConnectorIT.java    From xyz-hub with Apache License 2.0 5 votes vote down vote up
@Test
public void testCrudFeatureWithHash() throws Exception {
  // =========== INSERT ==========
  String insertJsonFile = "/events/InsertFeaturesEventWithHash.json";
  String insertResponse = invokeLambdaFromFile(insertJsonFile);
  String insertRequest = IOUtils.toString(GSContext.class.getResourceAsStream(insertJsonFile));
  assertRead(insertRequest, insertResponse, true);
  logger.info("Insert feature tested successfully");

  // =========== UPDATE ==========
  FeatureCollection featureCollection = XyzSerializable.deserialize(insertResponse);
  setPUUID(featureCollection);
  String featuresList = XyzSerializable.serialize(featureCollection.getFeatures(), new TypeReference<List<Feature>>() {
  });
  String updateRequest = "{\n" +
      "    \"type\": \"ModifyFeaturesEvent\",\n" +
      "    \"space\": \"foo\",\n" +
      "    \"enableUUID\": true,\n" +
      "    \"params\": {},\n" +
      "    \"updateFeatures\": " + featuresList + "\n" +
      "}";
  updateRequest = updateRequest.replaceAll("Tesla", "Honda");
  String updateResponse = invokeLambda(updateRequest);

  FeatureCollection responseCollection = XyzSerializable.deserialize(updateResponse);
  setPUUID(responseCollection);

  assertUpdate(updateRequest, updateResponse, true);
  assertUpdate(updateRequest, updateResponse, true);
  logger.info("Update feature tested successfully");
}