First you need to import the maven library of s3
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.10.26</version> </dependency>
The api code is as follows
public class AwsClient { static AmazonS3 s3; static TransferManager tx; private static String AWS_ACCESS_KEY = "AWS_ACCESS_KEY"; private static String AWS_SECRET_KEY = "AWS_SECRET_KEY "; static final String bucketName = "bucketName "; static { s3 = new AmazonS3Client(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)); Region usWest2 = Region.getRegion(Regions.CN_NORTH_1); s3.setRegion(usWest2); } /** * @Title: uploadToS3 * @Description: upload the file to S3 and return the url * @param @param tempFile target file * @param @param remoteFileName filename * @param @return * @param @throws IOException configuration file * @return String return type * @throws */ public static String uploadToS3(File tempFile, String remoteFileName) throws IOException { try { //upload files s3.putObject(new PutObjectRequest(bucketName, remoteFileName, tempFile).withCannedAcl(CannedAccessControlList.PublicRead)); // get a request GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest( bucketName, remoteFileName); //generate public url URL url = s3.generatePresignedUrl(urlRequest); System.out.println("=========URL=================" + url + "============URL============="); return url.toString(); } catch (AmazonServiceException ase) { ase.printStackTrace(); } catch (AmazonClientException ace) { ace.printStackTrace(); } return null; } /** * @Title: getContentFromS3 * @Description: Get file binary stream * @param @param remoteFileName * @param @throws IOException configuration file * @return S3ObjectInputStream return type data stream * @throws */ public static S3ObjectInputStream getContentFromS3(String remoteFileName) throws IOException { try { GetObjectRequest request = new GetObjectRequest(bucketName,remoteFileName); S3Object object = s3.getObject(request); S3ObjectInputStream inputStream = object.getObjectContent(); return inputStream; } catch (Exception e) { e.printStackTrace (); } return null; } /** * @Title: downFromS3 * @Description: Download the file to the local path * @param @param remoteFileName filename * @param @param path download path * @param @throws IOException configuration file * @return void return type * @throws */ public static void downFromS3(String remoteFileName,String path) throws IOException { try { GetObjectRequest request = new GetObjectRequest(bucketName,remoteFileName); s3.getObject(request,new File(path)); } catch (Exception e) { e.printStackTrace (); } } /** * @Title: getUrlFromS3 * @Description: get the url of the file * @param @param remoteFileName filename * @param @return * @param @throws IOException configuration file * @return String return type * @throws */ public static String getUrlFromS3(String remoteFileName) throws IOException { try { GeneratePresignedUrlRequest httpRequest=new GeneratePresignedUrlRequest(bucketName, remoteFileName); String url=s3.generatePresignedUrl(httpRequest).toString();//Temporary link return url; } catch (Exception e) { e.printStackTrace (); } return null; } /** * Verify that there is a Bucket named bucketName on s3 * @params3 * @param bucketName * @return */ public static boolean checkBucketExists(AmazonS3 s3, String bucketName) { List<Bucket> buckets = s3.listBuckets(); for (Bucket bucket : buckets) { if (Objects.equals(bucket.getName(), bucketName)) { return true; } } return false; } public static void delFromS3(String remoteFileName) throws IOException { try { s3.deleteObject(bucketName, remoteFileName); } catch (AmazonServiceException ase) { ase.printStackTrace(); } catch (AmazonClientException ace) { ace.printStackTrace(); } } public static void main(String[] args) throws Exception { String key = "redisinfo"; File tempFile = new File("C:\\Users\\guosen\\Desktop\\redis.txt"); uploadToS3(tempFile,key);//Upload file String url = getUrlFromS3(key);//Get the url of the file System.out.println(url); // delFromS3(key);//Delete the file }