Created
March 26, 2019 13:02
-
-
Save faizakram/171ec10b9e85b1295cbed7dbc2d17828 to your computer and use it in GitHub Desktop.
File Uploading, Deleting, listing
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
/** Add the Given Dependency | |
<dependency> | |
<groupId>com.amazonaws</groupId> | |
<artifactId>aws-java-sdk</artifactId> | |
<version>1.11.163</version> | |
</dependency> | |
*/ | |
import java.io.IOException; | |
import java.io.InputStream; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.AWSStaticCredentialsProvider; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
import com.amazonaws.regions.Regions; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |
import com.amazonaws.services.s3.model.DeleteObjectsRequest; | |
import com.amazonaws.services.s3.model.ObjectListing; | |
import com.amazonaws.services.s3.model.PutObjectResult; | |
import com.amazonaws.services.s3.model.S3Object; | |
public class S3Main { | |
/** | |
* Get Basic AWS Credentials | |
* @param accesskey | |
* @param secretkey | |
* @return | |
*/ | |
public AWSCredentials getBasicAWSCredentials(String accesskey, String secretkey) { | |
return new BasicAWSCredentials(accesskey, secretkey); | |
} | |
/** | |
* get AmazonS3 | |
* | |
* @return | |
*/ | |
public AmazonS3 getAmazonS3() { | |
return AmazonS3ClientBuilder.standard() | |
.withCredentials(new AWSStaticCredentialsProvider( | |
getBasicAWSCredentials("<AWS accesskey>", "<AWS secretkey>"))) | |
.withRegion(Regions.US_EAST_1).build(); | |
} | |
/** | |
* upload On S3 | |
* | |
* @param bucketName | |
* @param fileName | |
* @param file | |
* @return | |
*/ | |
public PutObjectResult uploadOnS3(String bucketName, String fileName, InputStream file) { | |
return getAmazonS3().putObject(bucketName, fileName, file, null); | |
} | |
/** | |
* get All Objects | |
* | |
* @param bucketName | |
* @return | |
*/ | |
public ObjectListing getAllObjects(String bucketName) { | |
return getAmazonS3().listObjects(bucketName); | |
} | |
/** | |
* download Object | |
* | |
* @param bucketName | |
* @param fileName | |
* @return | |
*/ | |
public S3Object downloadObject(String bucketName, String fileName) { | |
return getAmazonS3().getObject(bucketName, fileName); | |
} | |
/** | |
* Delete Object | |
* @param bucketName | |
* @param fileName | |
*/ | |
public void deleteObject(String bucketName, String fileName) { | |
getAmazonS3().deleteObject(bucketName, fileName); | |
} | |
/** | |
* Delete Multiple file | |
* @param bucketName | |
* @param fileNames | |
*/ | |
public void deleteMultipleObjects(String bucketName, String[] fileNames) { | |
DeleteObjectsRequest delObjReq = new DeleteObjectsRequest(bucketName) | |
.withKeys(fileNames); | |
getAmazonS3().deleteObjects(delObjReq); | |
} | |
public static void main(String[] args) throws IOException { | |
S3Main s3Main = new S3Main(); | |
String bucketName = "<BUCKET_NAME>"; | |
String path = "<PATH_NAME>"; | |
// Get All Object On S3 | |
/* | |
* ObjectListing objectListing = s3Main.getAllObjects(bucketName); for | |
* (S3ObjectSummary os : objectListing.getObjectSummaries()) { | |
* System.out.println(os.getKey()); } | |
*/ | |
// Uplaod File | |
/* | |
* File initialFile = new File("C:\\Users\\13615\\Downloads\\qq.jpeg"); | |
* InputStream targetStream = new FileInputStream(initialFile); PutObjectResult | |
* result = s3Main.uploadOnS3(bucketName, path+initialFile.getName(), | |
* targetStream); System.out.println(result.getContentMd5()); | |
* targetStream.close(); | |
*/ | |
//Download Object | |
/*S3Object s3object = s3Main.downloadObject(bucketName, path + "qq.jpeg"); | |
S3ObjectInputStream inputStream = s3object.getObjectContent(); | |
FileUtils.copyInputStreamToFile(inputStream, new File("E:\\ImageStore\\hello.jpeg"));*/ | |
//Delete Object | |
/*s3Main.deleteObject(bucketName, path + "qq.jpeg");*/ | |
//Delete Multiple File | |
/*String fileNames[] = {"<<filename1>>","<<filename2>>"}; | |
s3Main.deleteMultipleObjects(bucketName, fileNames);*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment