亚马逊Amazon S3 对象存储WEB服务接口

1.需要的依赖jar包

1 <dependency>

2   <groupId>com.amazonawsgroupId>

3   <artifactId>aws-java-sdk-s3artifactId>

4   <version>1.11.233version>

5 dependency>

2.访问参数实体

 1 public class AmazonS3Bean {
 2 
 3     private static String access_key_id = "access_key_id";// 访问密钥ID
 4     private static String secret_key = "secret_key";// 访问密钥
 5     private static String end_point = "end_point"; //访问IP及端口号
 6     private static String bucketname = "bucketname";//桶名
 7 
 8     private String storageobjectvopath = "";
 9 
10     public static String getAccess_key_id() {
11         return access_key_id;
12     }
13 
14     public static void setAccess_key_id(String access_key_id) {
15         AmazonS3Bean.access_key_id = access_key_id;
16     }
17 
18     public static String getSecret_key() {
19         return secret_key;
20     }
21 
22     public static void setSecret_key(String secret_key) {
23         AmazonS3Bean.secret_key = secret_key;
24     }
25 
26     public static String getEnd_point() {
27         return end_point;
28     }
29 
30     public static void setEnd_point(String end_point) {
31         AmazonS3Bean.end_point = end_point;
32     }
33 
34     public static String getBucketname() {
35         return bucketname;
36     }
37 
38     public void setBucketname(String bucketname) {
39         AmazonS3Bean.bucketname = bucketname;
40     }
41 
42     public String getStorageobjectvopath() {
43         return storageobjectvopath;
44     }
45 
46     public void setStorageobjectvopath(String storageobjectvopath) {
47         this.storageobjectvopath = storageobjectvopath;
48     }
49 
50 }

3.获取连接

 1 public class AmazonUtil {
 2     private static AmazonS3 conn;
 3     public static BasicAWSCredentials credentials;
 4 
 5     // 创建连接
 6     public void creatConnect(AmazonS3Bean s3bean) {
 7 
 8             credentials = new BasicAWSCredentials(AmazonS3Bean.getAccess_key_id(), AmazonS3Bean.getSecret_key());
 9 
10             ClientConfiguration 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); 15  conn.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 List buckets = 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 
 3     public static AmazonUtil Upload(AmazonS3Bean s3bean, MultipartFile file, String path) {
 4         //创建连接
 5         AmazonUtil amazonutil = new AmazonUtil();
 6         amazonutil.creatConnect(s3bean);
 7         //获取连接
 8         //获取桶
 9         Bucket bucket = amazonutil.getBuketsByname(s3bean.getBucketname());
10         if (bucket == null) {
11             //说明桶不存在,则创建一个默认的桶
12             bucket = amazonutil.creatBucket(s3bean.getBucketname());
13         }
14         //检查当前容器下该文件目录是否存在
15         try {
16             //上传文件对象到容器
17             amazonutil.creatObject(bucket.getName(), path + "/", file.getOriginalFilename(), file.getInputStream());
18         } catch (FileNotFoundException e) {
19             e.printStackTrace();
20             System.out.println("文件上传失败");
21         } catch (IOException e) {
22             e.printStackTrace();
23             System.out.println("文件上传失败");
24         }
25         return amazonutil;
26     }
27 
28 }

5.文件下载,将临时连接url返回给web页面

1  AmazonUtil amazonutil = new AmazonUtil();
2  amazonutil.creatConnect(new AmazonS3Bean());
3  GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(AmazonS3Bean.getBucketname(), filePath);
4  //临时链接
5  String url = amazonutil.getConnect().generatePresignedUrl(httpRequest).toString();

6.文件删除

1 AmazonUtil amazonutil = new AmazonUtil();
2 amazonutil.creatConnect(new AmazonS3Bean());
3 amazonutil.getConnect().deleteObject(AmazonS3Bean.getBucketname(), filePath);

 

转载于:https://www.cnblogs.com/HotDogHome/p/9467689.html

你可能感兴趣的:(亚马逊Amazon S3 对象存储WEB服务接口)