Created
May 17, 2021 06:24
-
-
Save learnit-codeit/44c76ed32a60e5443724603a10ee72f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AmazonS3Example { | |
private static final String ACCESS_KEY="<your access key here>"; | |
private static final String SECRET_KEY="<your secret key here>"; | |
private static final String SUFFIX = "/"; | |
private AmazonS3 initS3Client() { | |
AWSCredentials credentials = new BasicAWSCredentials( | |
ACCESS_KEY, | |
SECRET_KEY); | |
return new AmazonS3Client(credentials); | |
} | |
private void listAllBucket() { | |
System.out.println("********LISTING ALL BUCKETS - START*************"); | |
AmazonS3 s3client = initS3Client(); | |
for (Bucket bucket : s3client.listBuckets()) { | |
System.out.println(" * " + bucket.getName()); | |
} | |
System.out.println("********LISTING ALL BUCKETS - END *************"); | |
} | |
private void createFolder() { | |
System.out.println("********CREATING A FOLDER ON THE BUCKET anishantony - START*************"); | |
AmazonS3 s3client = initS3Client(); | |
String folderName = "test"; | |
String bucketName = "anishantony"; | |
ObjectMetadata metadata = new ObjectMetadata(); | |
metadata.setContentLength(0); | |
InputStream emptyContent = new ByteArrayInputStream(new byte[0]); | |
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, | |
folderName + SUFFIX, emptyContent, metadata); | |
s3client.putObject(putObjectRequest); | |
System.out.println("SUCCESS!!!! FOLDER ADDED."); | |
System.out.println("********CREATING A FOLDER ON THE BUCKET anishantony - END*************"); | |
} | |
private void addToBucket() { | |
System.out.println("********ADDING A FILE ON THE BUCKET anishantony - START*************"); | |
AmazonS3 s3client = initS3Client(); | |
String localFile = "C:\\img.jpg"; | |
String folderName = "test"; | |
String bucketName = "anishantony"; | |
String fileName = "img.jpg"; | |
s3client.putObject(new PutObjectRequest(bucketName, fileName, | |
new File(localFile))); | |
System.out.println("********ADDING A FILE ON THE BUCKET anishantony - END*************"); | |
} | |
private void retrieveEntry() throws IOException { | |
System.out.println("********RETRIEVING A FILE FROM THE BUCKET anishantony - START*************"); | |
String bucketName = "anishantony"; | |
String fileName = "test.txt"; | |
AmazonS3 s3client = initS3Client(); | |
S3Object object = s3client.getObject(new GetObjectRequest(bucketName, fileName)); | |
InputStream objectData = object.getObjectContent(); | |
// Process the objectData stream. | |
BufferedReader reader = new BufferedReader(new InputStreamReader(objectData)); | |
String s = null; | |
while ((s = reader.readLine()) != null) | |
{ | |
System.out.println(s); | |
} | |
objectData.close(); | |
System.out.println("********RETRIEVING A FILE FROM THE BUCKET anishantony - END*************"); | |
} | |
public static void main(String[] args) throws IOException { | |
AmazonS3Example example = new AmazonS3Example(); | |
example.listAllBucket(); | |
example.createFolder(); | |
example.addToBucket(); | |
example.retrieveEntry(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment