亚马逊Amazon|亚马逊Amazon S3 对象存储WEB服务接口
1.需要的依赖jar包
1
2
3
4
5
2.访问参数实体
1 public class AmazonS3Bean { 2 3private static String access_key_id = "access_key_id"; // 访问密钥ID 4private static String secret_key = "secret_key"; // 访问密钥 5private static String end_point = "end_point"; //访问IP及端口号 6private static String bucketname = "bucketname"; //桶名 7 8private String storageobjectvopath = ""; 9 10public static String getAccess_key_id() { 11return access_key_id; 12} 13 14public static void setAccess_key_id(String access_key_id) { 15AmazonS3Bean.access_key_id = access_key_id; 16} 17 18public static String getSecret_key() { 19return secret_key; 20} 21 22public static void setSecret_key(String secret_key) { 23AmazonS3Bean.secret_key = secret_key; 24} 25 26public static String getEnd_point() { 27return end_point; 28} 29 30public static void setEnd_point(String end_point) { 31AmazonS3Bean.end_point = end_point; 32} 33 34public static String getBucketname() { 35return bucketname; 36} 37 38public void setBucketname(String bucketname) { 39AmazonS3Bean.bucketname = bucketname; 40} 41 42public String getStorageobjectvopath() { 43return storageobjectvopath; 44} 45 46public void setStorageobjectvopath(String storageobjectvopath) { 47this.storageobjectvopath = storageobjectvopath; 48} 49 50 }
3.获取连接
1 public class AmazonUtil { 2private static AmazonS3 conn; 3public static BasicAWSCredentials credentials; 4 5// 创建连接 6public void creatConnect(AmazonS3Bean s3bean) { 7 8credentials = new BasicAWSCredentials(AmazonS3Bean.getAccess_key_id(), AmazonS3Bean.getSecret_key()); 9 10ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxConnections(500) 11.withProtocol(Protocol.HTTP).withRetryPolicy(PredefinedRetryPolicies.NO_RETRY_POLICY); 12 clientConfiguration.setSignerOverride("S3SignerType"); 13 14 conn = new AmazonS3Client(credentials, clientConfiguration); 15conn.setEndpoint(AmazonS3Bean.getEnd_point()); 16 17} 18 19 // 获取连接 20 public AmazonS3 getConnect() { 21 return conn; 22} 23 24 // 创建存储桶是否存在 25 public Bucket createBucket(String bucketName) { 26 if (bucketName.isEmpty()) { 27 return null; 28} 29 Bucket bucket = conn.createBucket(bucketName); 30 31 return bucket; 32} 33 34 // 查询存储桶是否存在 35 public Bucket getBuketsByname(String bucketName) { 36 Bucket resultBucket = null; 37 if (bucketName.isEmpty()) { 38 return null; 39} 40 Listbuckets = conn.listBuckets(); 41 if (buckets == null) { 42 return resultBucket; 43} 44 for (Bucket bucket : buckets) { 45 if (bucketName.equals(bucket.getName())) { 46 resultBucket = bucket; 47 break; 48} 49} 50 return resultBucket; 51} 52 53 public PutObjectResult createObject(String bucketName, String StorageObjectVoPath, String fileName, InputStream inputStream) 54 throws FileNotFoundException { 55 if (StorageObjectVoPath == null || StorageObjectVoPath.isEmpty() || "null".equals(StorageObjectVoPath)) { 56 StorageObjectVoPath = ""; 57} 58 if (inputStream == null) { 59 return null; 60} 61 String fileAllPath = StorageObjectVoPath + fileName; 62 63 PutObjectResult result = conn.putObject(bucketName, fileAllPath, inputStream, new ObjectMetadata()); 64 return result; 65} 66 67 }
4.上传文件
1 public class AmazonS3Upload { 2 3public static AmazonUtil Upload(AmazonS3Bean s3bean, MultipartFile file, String path) { 4//创建连接 5AmazonUtil amazonutil = new AmazonUtil(); 6amazonutil.creatConnect(s3bean); 7//获取连接 8//获取桶 9Bucket bucket = amazonutil.getBuketsByname(s3bean.getBucketname()); 10if (bucket == null) { 11//说明桶不存在,则创建一个默认的桶 12bucket = amazonutil.creatBucket(s3bean.getBucketname()); 13} 14//检查当前容器下该文件目录是否存在 15try { 16//上传文件对象到容器 17amazonutil.creatObject(bucket.getName(), path + "/", file.getOriginalFilename(), file.getInputStream()); 18} catch (FileNotFoundException e) { 19e.printStackTrace(); 20System.out.println("文件上传失败"); 21} catch (IOException e) { 22e.printStackTrace(); 23System.out.println("文件上传失败"); 24} 25return amazonutil; 26} 27 28 }
5.文件下载,将临时连接url返回给web页面
1AmazonUtil amazonutil = new AmazonUtil(); 2amazonutil.creatConnect(new AmazonS3Bean()); 3GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(AmazonS3Bean.getBucketname(), filePath); 4//临时链接 5String url = amazonutil.getConnect().generatePresignedUrl(httpRequest).toString();
6.文件删除
1 AmazonUtil amazonutil = new AmazonUtil(); 2 amazonutil.creatConnect(new AmazonS3Bean()); 3 amazonutil.getConnect().deleteObject(AmazonS3Bean.getBucketname(), filePath);
【亚马逊Amazon|亚马逊Amazon S3 对象存储WEB服务接口】转载于:https://www.cnblogs.com/HotDogHome/p/9467689.html
推荐阅读
- 数组常用方法一
- jQuery插件
- iOS面试题--基础
- 口红选得好,对象不愁找......
- java静态代理模式
- Python-类和对象
- JavaScript|JavaScript: BOM对象 和 DOM 对象的增删改查
- JavaScript|JavaScript — call()和apply()、Date对象、Math、包装类、字符串的方法
- 14--Python|14--Python 面向对象进阶
- vue_day05