SHARE
TWEET

Untitled

a guest May 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         <dependency>
  2.             <groupId>commons-io</groupId>
  3.             <artifactId>commons-io</artifactId>
  4.             <version>2.5</version>
  5.         </dependency>
  6.  
  7.     private static final String PATH_BASE = "//Users/farithjhg/Desktop/";
  8.     private static final String MINIO_SECRETACCESSKEY = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
  9.     private static final String MINIO_ACCESSKEYID = "AKIAIOSFODNN7EXAMPLE";
  10.    
  11.     private static final String AWS_SECRET_KEY = "Ii4Wkn8bMiyJAh2FM/ZLB7/MOusKsXO/O2DHU2WQ";
  12.     private static final String AWS_ACCESS_KEY = "AKIA4WLFPEEI2YBY5IMW";
  13.  
  14.     private static AmazonS3 accesingMyS3() {
  15.         AWSCredentials credentials = new BasicAWSCredentials(MINIO_ACCESSKEYID, MINIO_SECRETACCESSKEY);
  16.         ClientConfiguration clientConfiguration = new ClientConfiguration();
  17.         clientConfiguration.setSignerOverride("AWSS3V4SignerType");
  18.  
  19.         AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  20.                 .withEndpointConfiguration(
  21.                         new AwsClientBuilder.EndpointConfiguration("http://localhost:9001", Regions.US_EAST_1.name()))
  22.                 .withPathStyleAccessEnabled(true).withClientConfiguration(clientConfiguration)
  23.                 .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
  24.         log.info("Listing Buckets from MinIO");
  25.         listBuckets(s3Client);
  26.         String bucketName = "aml-s3-b1";
  27.         // createBucket(s3Client, bucketName );
  28.         // listBuckets(s3Client);
  29.         listingObjects(s3Client, bucketName);
  30.         String keyName = "openapi.yaml";
  31.         try {
  32.             downloadingObjects(s3Client, bucketName, keyName);
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         }
  36.         String keyName2 = "newswagger.yaml";
  37.         File fileName = new File(PATH_BASE + keyName2);
  38.         uploadingFile(s3Client, bucketName, fileName);
  39.         listingObjects(s3Client, bucketName);
  40.         log.info("getObjectAsString::"+keyName);
  41.         log.info(getObjectAsString(s3Client, bucketName, keyName));
  42.         return s3Client;
  43.     }
  44.  
  45.     private static void accessingS3() {
  46.         AWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
  47.  
  48.         AmazonS3 s3client = AmazonS3ClientBuilder.standard()
  49.                 .withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_2).build();
  50.         log.info("Listing Buckets from AWS");
  51.         listBuckets(s3client);
  52.     }
  53.  
  54.     private static void listBuckets(AmazonS3 s3client) {
  55.         List<Bucket> buckets = s3client.listBuckets();
  56.         for (Bucket bucket : buckets) {
  57.             log.info(bucket.getName());
  58.         }
  59.     }
  60.  
  61.     private static void createBucket(AmazonS3 s3client, String bucketName) {
  62.         if (s3client.doesBucketExistV2(bucketName)) {
  63.             log.info("Bucket name is not available." + " Try again with a different Bucket name.");
  64.             return;
  65.         }
  66.  
  67.         s3client.createBucket(bucketName);
  68.     }
  69.  
  70.     private static void deletingBuckets(AmazonS3 s3client, String bucketName) {
  71.         try {
  72.             s3client.deleteBucket(bucketName);
  73.         } catch (AmazonServiceException e) {
  74.             log.error(e.getErrorMessage());
  75.             return;
  76.         }
  77.     }
  78.  
  79.     private static void uploadingFile(AmazonS3 s3client, String bucketName, File fileName) {
  80.         s3client.putObject(bucketName, fileName.getName(), fileName);
  81.     }
  82.    
  83.     private static String getObjectAsString(AmazonS3 s3client, String bucketName, String keyName) {
  84.         return s3client.getObjectAsString(bucketName,keyName);
  85.     }
  86.  
  87.     private static void listingObjects(AmazonS3 s3client, String bucketName) {
  88.         ObjectListing objectListing = s3client.listObjects(bucketName);
  89.         for (S3ObjectSummary os : objectListing.getObjectSummaries()) {
  90.             log.info(os.getKey() + "::Size:" + os.getSize());
  91.         }
  92.     }
  93.  
  94.     private static void downloadingObjects(AmazonS3 s3client, String bucketName, String keyName) throws IOException {
  95.         S3Object s3object = s3client.getObject(bucketName, keyName);
  96.         S3ObjectInputStream inputStream = s3object.getObjectContent();
  97.         FileUtils.copyInputStreamToFile(inputStream, new File(PATH_BASE + keyName));
  98.     }
  99.  
  100.     private static void copyingFromBucket1ToBucket2(AmazonS3 s3client, String bucketName1, String keyName1,
  101.             String bucketName2, String keyName2) {
  102.         s3client.copyObject(bucketName1, keyName1, bucketName2, keyName2);
  103.  
  104.     }
  105.  
  106.     private static void deletingObjectFromBucket(AmazonS3 s3client, String bucketName, String keyName) {
  107.         s3client.deleteObject(bucketName, keyName);
  108.  
  109.     }
  110.  
  111.     private static void deletingMultipleObjects(AmazonS3 s3client, String bucketName, String[] objkeyArr) {
  112.         DeleteObjectsRequest delObjReq = new DeleteObjectsRequest(bucketName).withKeys(objkeyArr);
  113.         s3client.deleteObjects(delObjReq);
  114.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top