Search in sources :

Example 11 with AmazonClientException

use of com.amazonaws.AmazonClientException in project hadoop by apache.

the class AWSCredentialProviderList method getCredentials.

/**
   * Iterate through the list of providers, to find one with credentials.
   * If {@link #reuseLastProvider} is true, then it is re-used.
   * @return a set of credentials (possibly anonymous), for authenticating.
   */
@Override
public AWSCredentials getCredentials() {
    checkNotEmpty();
    if (reuseLastProvider && lastProvider != null) {
        return lastProvider.getCredentials();
    }
    AmazonClientException lastException = null;
    for (AWSCredentialsProvider provider : providers) {
        try {
            AWSCredentials credentials = provider.getCredentials();
            if ((credentials.getAWSAccessKeyId() != null && credentials.getAWSSecretKey() != null) || (credentials instanceof AnonymousAWSCredentials)) {
                lastProvider = provider;
                LOG.debug("Using credentials from {}", provider);
                return credentials;
            }
        } catch (AmazonClientException e) {
            lastException = e;
            LOG.debug("No credentials provided by {}: {}", provider, e.toString(), e);
        }
    }
    // no providers had any credentials. Rethrow the last exception
    // or create a new one.
    String message = "No AWS Credentials provided by " + listProviderNames();
    if (lastException != null) {
        message += ": " + lastException;
    }
    throw new AmazonClientException(message, lastException);
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 12 with AmazonClientException

use of com.amazonaws.AmazonClientException in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createTable.

/**
     * Create table (if not present) and wait for table to become active.
     *
     * Synchronized in order to ensure that at most single thread is creating the table at a time
     *
     * @param mapper
     * @param dtoClass
     * @return whether table creation succeeded.
     */
private synchronized boolean createTable(DynamoDBMapper mapper, Class<?> dtoClass) {
    if (db == null) {
        return false;
    }
    String tableName;
    try {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(dbConfig.getReadCapacityUnits(), dbConfig.getWriteCapacityUnits());
        CreateTableRequest request = mapper.generateCreateTableRequest(dtoClass);
        request.setProvisionedThroughput(provisionedThroughput);
        if (request.getGlobalSecondaryIndexes() != null) {
            for (GlobalSecondaryIndex index : request.getGlobalSecondaryIndexes()) {
                index.setProvisionedThroughput(provisionedThroughput);
            }
        }
        tableName = request.getTableName();
        try {
            db.getDynamoClient().describeTable(tableName);
        } catch (ResourceNotFoundException e) {
            // No table present, continue with creation
            db.getDynamoClient().createTable(request);
        } catch (AmazonClientException e) {
            logger.error("Table creation failed due to error in describeTable operation", e);
            return false;
        }
        // table found or just created, wait
        return waitForTableToBecomeActive(tableName);
    } catch (AmazonClientException e) {
        logger.error("Exception when creating table", e);
        return false;
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)

Example 13 with AmazonClientException

use of com.amazonaws.AmazonClientException in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method store.

/**
     * {@inheritDoc}
     */
@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        logger.debug("Undefined item state received. Not storing item.");
        return;
    }
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return;
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return;
    }
    String realName = item.getName();
    String name = (alias != null) ? alias : realName;
    Date time = new Date(System.currentTimeMillis());
    State state = item.getState();
    logger.trace("Tried to get item from item class {}, state is {}", item.getClass(), state.toString());
    DynamoDBItem<?> dynamoItem = AbstractDynamoDBItem.fromState(name, state, time);
    DynamoDBMapper mapper = getDBMapper(tableNameResolver.fromItem(dynamoItem));
    if (!createTable(mapper, dynamoItem.getClass())) {
        logger.warn("Table creation failed. Not storing item");
        return;
    }
    try {
        logger.debug("storing {} in dynamo. Serialized value {}. Original Item: {}", name, state, item);
        mapper.save(dynamoItem);
        logger.debug("Sucessfully stored item {}", item);
    } catch (AmazonClientException e) {
        logger.error("Error storing object to dynamo: {}", e.getMessage());
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Date(java.util.Date)

Example 14 with AmazonClientException

use of com.amazonaws.AmazonClientException in project ORCID-Source by ORCID.

the class S3MessageProcessor method update_1_2_API.

private void update_1_2_API(String orcid) {
    if (is12IndexingEnabled) {
        try {
            OrcidMessage profile = orcid12ApiClient.fetchPublicProfile(orcid);
            // Update API 1.2
            if (profile != null) {
                s3Updater.updateS3(orcid, profile);
                recordStatusManager.markAsSent(orcid, AvailableBroker.DUMP_STATUS_1_2_API);
            }
        } catch (LockedRecordException | DeprecatedRecordException e) {
            try {
                if (e instanceof LockedRecordException) {
                    LOG.error("Record " + orcid + " is locked");
                    exceptionHandler.handle12LockedRecordException(orcid, ((LockedRecordException) e).getOrcidMessage());
                } else {
                    LOG.error("Record " + orcid + " is deprecated");
                    exceptionHandler.handle12DeprecatedRecordException(orcid, ((DeprecatedRecordException) e).getOrcidDeprecated());
                }
                recordStatusManager.markAsSent(orcid, AvailableBroker.DUMP_STATUS_1_2_API);
            } catch (JsonProcessingException | AmazonClientException | JAXBException e1) {
                LOG.error("Unable to handle LockedRecordException for record " + orcid, e1);
                recordStatusManager.markAsFailed(orcid, AvailableBroker.DUMP_STATUS_1_2_API);
            }
        } catch (Exception e) {
            // something else went wrong fetching record from ORCID and
            // threw a
            // runtime exception
            LOG.error("Unable to fetch record " + orcid + " for 1.2 API");
            LOG.error(e.getMessage(), e);
            recordStatusManager.markAsFailed(orcid, AvailableBroker.DUMP_STATUS_1_2_API);
        }
    }
}
Also used : LockedRecordException(org.orcid.listener.exception.LockedRecordException) DeprecatedRecordException(org.orcid.listener.exception.DeprecatedRecordException) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) LockedRecordException(org.orcid.listener.exception.LockedRecordException) DeprecatedRecordException(org.orcid.listener.exception.DeprecatedRecordException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JAXBException(javax.xml.bind.JAXBException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 15 with AmazonClientException

use of com.amazonaws.AmazonClientException in project jackrabbit-oak by apache.

the class S3Backend method init.

public void init(CachingDataStore store, String homeDir, Properties prop) throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        this.store = store;
        s3ReqDecorator = new S3RequestDecorator(prop);
        s3service = Utils.openService(prop);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = prop.getProperty(S3Constants.S3_BUCKET);
            // Alternately check if the 'container' property is set
            if (Strings.isNullOrEmpty(bucket)) {
                bucket = prop.getProperty(S3Constants.S3_CONTAINER);
            }
        }
        String region = prop.getProperty(S3Constants.S3_REGION);
        Region s3Region = null;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }
        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }
        int writeThreads = 10;
        String writeThreadsStr = prop.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, (ThreadPoolExecutor) Executors.newFixedThreadPool(writeThreads, new NamedThreadFactory("s3-transfer-manager-worker")));
        int asyncWritePoolSize = 10;
        String maxConnsStr = prop.getProperty(S3Constants.S3_MAX_CONNS);
        if (maxConnsStr != null) {
            asyncWritePoolSize = Integer.parseInt(maxConnsStr) - writeThreads;
        }
        asyncWriteExecuter = (ThreadPoolExecutor) Executors.newFixedThreadPool(asyncWritePoolSize, new NamedThreadFactory("s3-write-worker"));
        String renameKeyProp = prop.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        Map<String, String> filteredMap = Maps.newHashMap();
        if (prop != null) {
            filteredMap = Maps.filterKeys(Maps.fromProperties(prop), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) NamedThreadFactory(org.apache.jackrabbit.core.data.util.NamedThreadFactory) AmazonClientException(com.amazonaws.AmazonClientException) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) S3RequestDecorator(org.apache.jackrabbit.oak.blob.cloud.s3.S3RequestDecorator) Region(com.amazonaws.services.s3.model.Region) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Map(java.util.Map)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)202 IOException (java.io.IOException)70 AmazonServiceException (com.amazonaws.AmazonServiceException)32 ArrayList (java.util.ArrayList)32 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)23 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)19 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)17 HashMap (java.util.HashMap)16 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)14 Test (org.junit.Test)14 SienaException (siena.SienaException)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)11 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)11 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 InterruptedIOException (java.io.InterruptedIOException)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)9 File (java.io.File)9