Search in sources :

Example 1 with BackblazeCredentialsException

use of org.datatransferproject.datatransfer.backblaze.exception.BackblazeCredentialsException in project data-transfer-project by google.

the class BackblazeDataTransferClient method init.

public void init(String keyId, String applicationKey, String exportService) throws BackblazeCredentialsException, IOException {
    // Fetch all the available buckets and use that to find which region the user is in
    ListBucketsResponse listBucketsResponse = null;
    String userRegion = null;
    // The Key ID starts with the region identifier number, so reorder the regions such that
    // the first region is most likely the user's region
    String regionId = keyId.substring(0, 3);
    BACKBLAZE_REGIONS.sort((String region1, String region2) -> {
        if (region1.endsWith(regionId)) {
            return -1;
        }
        return 0;
    });
    Throwable s3Exception = null;
    for (String region : BACKBLAZE_REGIONS) {
        try {
            s3Client = backblazeS3ClientFactory.createS3Client(keyId, applicationKey, region);
            listBucketsResponse = s3Client.listBuckets();
            userRegion = region;
            break;
        } catch (S3Exception e) {
            s3Exception = e;
            if (s3Client != null) {
                s3Client.close();
            }
            if (e.statusCode() == 403) {
                monitor.debug(() -> String.format("User is not in region %s", region));
            }
        }
    }
    if (listBucketsResponse == null || userRegion == null) {
        throw new BackblazeCredentialsException("User's credentials or permissions are not valid for any regions available", s3Exception);
    }
    bucketName = getOrCreateBucket(s3Client, listBucketsResponse, userRegion, exportService);
}
Also used : ListBucketsResponse(software.amazon.awssdk.services.s3.model.ListBucketsResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) BackblazeCredentialsException(org.datatransferproject.datatransfer.backblaze.exception.BackblazeCredentialsException)

Aggregations

BackblazeCredentialsException (org.datatransferproject.datatransfer.backblaze.exception.BackblazeCredentialsException)1 ListBucketsResponse (software.amazon.awssdk.services.s3.model.ListBucketsResponse)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1