AWS S3使用demo---java篇

yiranpiaoluo 2017-04-26

AWS的api过于繁琐,入门者没必要去全文阅读api,我这里简单封装S3的api,方便大家上手使用s3。

首先需要引入s3的maven库

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk</artifactId>
  <version>1.10.26</version>
</dependency>

api代码如下

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: 将文件上传至S3上并且返回url
    * @param @param tempFile 目标文件
    * @param @param remoteFileName 文件名
    * @param @return
    * @param @throws IOException    设定文件 
    * @return String    返回类型 
    * @throws 
    */
    public static String uploadToS3(File tempFile, String remoteFileName) throws IOException {
        try {
            //上传文件
            s3.putObject(new PutObjectRequest(bucketName, remoteFileName, tempFile).withCannedAcl(CannedAccessControlList.PublicRead));
            //获取一个request
            GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                    bucketName, remoteFileName);
            //生成公用的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: 获取文件2进制流
    * @param @param remoteFileName
    * @param @throws IOException    设定文件 
    * @return S3ObjectInputStream    返回类型  数据流
    * @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: 将文件下载到本地路径
    * @param @param remoteFileName 文件名
    * @param @param path 下载的路径
    * @param @throws IOException    设定文件 
    * @return void    返回类型 
    * @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: 获取文件的url 
    * @param @param remoteFileName 文件名
    * @param @return
    * @param @throws IOException    设定文件 
    * @return String    返回类型 
    * @throws 
    */
    public static String getUrlFromS3(String remoteFileName) throws IOException {
		try {
			GeneratePresignedUrlRequest httpRequest=new GeneratePresignedUrlRequest(bucketName, remoteFileName);
			String url=s3.generatePresignedUrl(httpRequest).toString();//临时链接
			return url;
		} catch (Exception e) {
    	    e.printStackTrace();
		} 
		return null;
    }
    /**
     * 验证s3上是否存在名称为bucketName的Bucket
     * @param s3
     * @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);//上传文件
    	String url  = getUrlFromS3(key);//获取文件的url
    	System.out.println(url);
//    	delFromS3(key);//删除文件
    }

相关推荐