|
| 1 | +package software.xdev; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.eclipse.store.afs.blobstore.types.BlobStoreFileSystem; |
| 7 | +import org.eclipse.store.storage.embedded.types.EmbeddedStorage; |
| 8 | +import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import com.ibm.cloud.objectstorage.ClientConfiguration; |
| 13 | +import com.ibm.cloud.objectstorage.auth.AWSCredentials; |
| 14 | +import com.ibm.cloud.objectstorage.auth.AWSStaticCredentialsProvider; |
| 15 | +import com.ibm.cloud.objectstorage.client.builder.AwsClientBuilder; |
| 16 | +import com.ibm.cloud.objectstorage.oauth.BasicIBMOAuthCredentials; |
| 17 | +import com.ibm.cloud.objectstorage.services.s3.AmazonS3; |
| 18 | +import com.ibm.cloud.objectstorage.services.s3.AmazonS3ClientBuilder; |
| 19 | + |
| 20 | +import software.xdev.eclipse.store.afs.ibm.cos.types.CosConnector; |
| 21 | + |
| 22 | + |
| 23 | +@SuppressWarnings("checkstyle:MagicNumber") |
| 24 | +public final class Application |
| 25 | +{ |
| 26 | + private static final String COS_ENDPOINT = ""; // eg "https://s3.us.cloud-object-storage.appdomain.cloud" |
| 27 | + private static final String COS_API_KEY_ID = ""; // eg "0viPHOY7LbLNa9eLftrtHPpTjoGv6hbLD1QalRXikliJ" |
| 28 | + private static final String COS_SERVICE_CRN = ""; |
| 29 | + // "crn:v1:bluemix:public:cloud-object-storage:global:a/<CREDENTIAL_ID_AS_GENERATED>:<SERVICE_ID_AS_GENERATED>::" |
| 30 | + private static final String COS_BUCKET_LOCATION = ""; // eg "us" |
| 31 | + private static final String BUCKET_NAME = ""; |
| 32 | + |
| 33 | + private static final Logger LOG = LoggerFactory.getLogger(Application.class); |
| 34 | + |
| 35 | + /** |
| 36 | + * This function connects to the IBM COS and writes one million String-Entries on it. |
| 37 | + */ |
| 38 | + public static void main(final String[] args) |
| 39 | + { |
| 40 | + final List<String> stringList = new ArrayList<>(); |
| 41 | + LOG.info("List size before loading: {}", stringList.size()); |
| 42 | + try(final EmbeddedStorageManager manager = getStorageManager(stringList)) |
| 43 | + { |
| 44 | + LOG.info("List size after loading: {}", stringList.size()); |
| 45 | + for(int i = 0; i < 1_000_000; i++) |
| 46 | + { |
| 47 | + stringList.add("Test" + i); |
| 48 | + } |
| 49 | + manager.store(stringList); |
| 50 | + LOG.info("List size after storing new entities: {}", stringList.size()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static EmbeddedStorageManager getStorageManager(final Object root) |
| 55 | + { |
| 56 | + final AmazonS3 client = createClient(COS_API_KEY_ID, COS_SERVICE_CRN, COS_ENDPOINT, COS_BUCKET_LOCATION); |
| 57 | + |
| 58 | + LOG.info("Start creating file system"); |
| 59 | + final BlobStoreFileSystem cloudFileSystem = BlobStoreFileSystem.New( |
| 60 | + // use caching connector |
| 61 | + CosConnector.Caching(client) |
| 62 | + ); |
| 63 | + LOG.info("Finished creating file system"); |
| 64 | + |
| 65 | + LOG.info("Starting storage manager"); |
| 66 | + final EmbeddedStorageManager storageManager = EmbeddedStorage.start( |
| 67 | + root, |
| 68 | + cloudFileSystem.ensureDirectoryPath(BUCKET_NAME)); |
| 69 | + LOG.info("Finished storage manager"); |
| 70 | + |
| 71 | + return storageManager; |
| 72 | + } |
| 73 | + |
| 74 | + public static AmazonS3 createClient( |
| 75 | + final String apiKey, |
| 76 | + final String serviceInstanceId, |
| 77 | + final String endpointUrl, |
| 78 | + final String location) |
| 79 | + { |
| 80 | + LOG.info("Start creating client"); |
| 81 | + final AWSCredentials credentials = new BasicIBMOAuthCredentials(apiKey, serviceInstanceId); |
| 82 | + final ClientConfiguration clientConfig = new ClientConfiguration().withRequestTimeout(-1); |
| 83 | + clientConfig.setUseTcpKeepAlive(true); |
| 84 | + |
| 85 | + final AmazonS3 build = AmazonS3ClientBuilder.standard() |
| 86 | + .withCredentials(new AWSStaticCredentialsProvider(credentials)) |
| 87 | + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpointUrl, location)) |
| 88 | + .withPathStyleAccessEnabled(true) |
| 89 | + .withClientConfiguration(clientConfig) |
| 90 | + .build(); |
| 91 | + LOG.info("Finished creating client"); |
| 92 | + return build; |
| 93 | + } |
| 94 | + |
| 95 | + private Application() |
| 96 | + { |
| 97 | + } |
| 98 | +} |
0 commit comments