aws s3

Bismillah
1) read from S3
2)write from S3

---

  com.amazonaws
  aws-java-sdk
  1.11.163

 --
 S3(Simple Storage Service)

 upload, download, copy, move, rename, delete objectrs
 --
 each bucket can store any number of objects
 ---
 AWS security Credentials
  AWS API ACTions need
   access key section of Security Credentials page
   IAM console IAM user credetnaisl
  ---

  1) Creating client connection

  AWSCredentials credentials = new BasicAWSCredentials("");

  AmazonS3 s3Client = AmazonS3ClientBuilder
  .standard()
  .withCredentials(new AWSStaticCredentialsProvider(credentials))
  .withRegion(Regions.US_EAST-2)
  .build();
  ---
  bucket name unique across amazon

  bucket name
   no underscore
   between 3 to 63 chars long
   not end with dash
   no adjacnet perio
   no dashes next to period
   no upper case letters

  ---
  2) create bucket
  String bucket-name = "mi-bucket";
  if( s3client.doestBucketExist(bucketName)) {
  Log.info("Bucket name is not available.", )
  }
---
List buckets = s3client.listBuckets();
for(Bucket bucket: buckets) {
System.out.println(bucket.getName());
}
---
3) delete bucket
s3Client.deleteBucket("mi-bucket")
AmazonServiceException e
e.getErrorMessage()
---

4) uploading objects
bucketname
key: full path to file
file: actual file containing the data to be uploaded

s3client.putObject(
 bucketName,
 "Documetns/hello.txt",
  new File("/Users/user/Document/hello.txt"));

 5) listing objects
 ObjectListing objectListing = s3client.listObjects(bucketName);
 for(s3ObjectSummary : objectListing.getObjectSummaries()) {
  LOG.info(os.getKey());
 }
 ---
 6) Downloading an object
 S3Object s3object = s3client.getObject(bucketName, "/picture/pic.png");
 S3ObjectInputStream inputStream = s3object.getObjectContent();
 FileUtils.copyInputStreamToFile(inputStream
 , new File("/Users/user/desktop/hello.png"));
 --

 7) copy object
 source bucket name
 object key in source bucket
 destination bucket name
object key in destination bucket

8) delete an object
s3client.deleteOBject("", "picute/pic.png")

multiple
DeleteObjectRequestz("mi-bucket")
withKeys({"docuement/hello.txt"})

No comments:

Post a Comment