AWS S3服务文件上传和下载

1. 引入S3依赖
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
</dependency>
2. 配置准备

accessKey:公钥
secretKey:密钥
region:地区
buketName:桶

3. 初始化配置
@Bean
public AmazonS3 getAmazonS3() {
     AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
     AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
             .withRegion(region)
             .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
             .withPathStyleAccessEnabled(true)
             .withClientConfiguration(new ClientConfiguration())
             .build();
     return amazonS3;
}
4. 文件上传
ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(multipartFile.getContentType());
            objectMetadata.setContentLength(multipartFile.getSize());
            String simpleUUID = IdUtil.simpleUUID();
            PutObjectResult putObjectResult = amazonS3.putObject(new PutObjectRequest(bucketName, simpleUUID, multipartFile.getInputStream(), objectMetadata));
            //PutObjectResult putObjectResult = amazonS3.putObject(new PutObjectRequest(bucketName, simpleUUID, multipartFile.getInputStream(), objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
            return "https://" + bucketName + ".s3-" + amazonS3.getRegionName() + ".amazonaws.com/" + URLEncoder.encode(simpleUUID);
5. 文件下载

方法一:

public static final byte[] download(String bucketName, String fileName) {
    ClientConfiguration baseOpts = new ClientConfiguration();
    baseOpts.setSignerOverride("S3SignerType");
    AWSCredentials credentials = new BasicAWSCredentials(ACCESSKEY, SECRETKEY);
    AmazonS3 client = new AmazonS3Client(credentials, baseOpts);
    client.setEndpoint(HOSTNAME);
    S3ClientOptions s3Opts = new S3ClientOptions();
    s3Opts.setPathStyleAccess(true);
    client.setS3ClientOptions(s3Opts);

    S3Object downloadObject = client.getObject(new GetObjectRequest(bucketName, fileName));
    S3ObjectInputStream downloadObjectInput = downloadObject.getObjectContent();
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    try {
        while ((length = downloadObjectInput.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toByteArray();
    } catch (IOException e1) {
        log.error("S3 File download error,error:{}", e1);
    } finally {
        try {
            downloadObjectInput.close();
            result.close();
        } catch (IOException e2) {
            log.error("S3 File download error,error:{}", e2);
        }
    }
    return null;
}

方法二:

public  void amazonS3Downloading(String key,String targetFilePath) {

        S3Object object = s3.getAmazonS3().getObject(new GetObjectRequest(bucketName, key));
        if (object != null) {
            log.info("Content-Type: " + object.getObjectMetadata().getContentType());
            InputStream input = null;
            FileOutputStream fileOutputStream = null;
            byte[] data = null;
            try {
                //获取文件流
                input = object.getObjectContent();
                data = new byte[input.available()];
                int len = 0;
                fileOutputStream = new FileOutputStream(targetFilePath);
                while ((len = input.read(data)) != -1) {
                    fileOutputStream.write(data, 0, len);
                }
                log.info("Downloading the file succeeded");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

以下是实际使用案例
pom.xml

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.10</version>
</dependency>

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

application.yml

aws:
  accessKey: 公钥
  secretKey: 私钥
  region: 地区
  hostName: endPoint信息
  baseBucketName: 桶名
  isLoadHostName: true

AwsConfig.java

/**
 * @author ght
 * @create 2021/12/08 13:26
 */
@Component
@ConfigurationProperties(prefix = "aws")
@Slf4j
public class AwsConfig {
    @Value("${aws.accessKey}")
    private String accessKey;

    @Value("${aws.secretKey}")
    private String secretKey;

    @Value("${aws.region}")
    private String region;

    @Value("${aws.hostName}")
    private String hostName;

    @Value("${aws.baseBucketName}")
    private String baseBucketName;

    @Value("${aws.isLoadHostName}")
    private Boolean isLoadHostName;

    @Bean
    public AmazonS3 getAmazonS3() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration baseOpts = new ClientConfiguration();
        /*if(isLoadHostName == null || isLoadHostName) {
            baseOpts.setSignerOverride("S3SignerType");
            baseOpts.setProtocol(Protocol.HTTPS);
        }*/
        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
//                .withRegion(region)
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(hostName, region))  // 如果有endpoint,可以用这个,这个和withRegion(Region)不能一起使用
                .withPathStyleAccessEnabled(true)  // 如果配置了S3域名,就需要加这个进行路径访问,要不然会报AccessKey不存在的问题
                .withClientConfiguration(baseOpts)
                .build();
//        amazonS3.setEndpoint(hostName);
        /*if(isLoadHostName == null || isLoadHostName){
            amazonS3.setEndpoint(hostName);
        }*/
        return amazonS3;

        /*ClientConfiguration baseOpts = new ClientConfiguration();
        baseOpts.setSignerOverride("S3SignerType");
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonS3 client = new AmazonS3Client(credentials, baseOpts);
        client.setEndpoint(hostName);
        S3ClientOptions s3Opts = new S3ClientOptions();
        s3Opts.setPathStyleAccess(true);
        client.setS3ClientOptions(s3Opts);
        return client;*/
    }


//    @Bean
//    public AmazonSimpleEmailService getAmazonSimpleEmailService() {
//        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
//
//        AmazonSimpleEmailService amazonSimpleEmailService = AmazonSimpleEmailServiceClientBuilder.standard()
//                .withRegion("eu-central-1")
//                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
//                .withClientConfiguration(new ClientConfiguration())
//                .build();
//        return amazonSimpleEmailService;
//    }
//
//
//    @Bean
//    public AmazonSNS getAmazonSNS(){
//        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
//        AmazonSNS amazonSNS = AmazonSNSClientBuilder.standard()
//                .withRegion("eu-central-1")
//                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
//                .withClientConfiguration(new ClientConfiguration())
//                .build();
//        return amazonSNS;
//    }
}

AwsService.java

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

public interface AwsService {

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @param uploadPath 目标路径
     * @return
     */
    String uploadFile(File file, String uploadPath);

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    String uploadMultipartFile(MultipartFile multipartFile, String uploadPath);

    /**
     * 下载文件
     *
     * @param url
     * @return
     */
    String downloadFile(String url);

	/**
     * key:上传的文件,如:/aa/123.png 表示将123.png放到aa目录下的
     * targetFilePath:下载的目标地址,如果是Linux建议用/tmp
     *
     * @param key
     * @param targetFilePath
     */
	void amazonS3Downloading(String key,String targetFilePath);

    /**
     * 创建桶
     *
     * @param bucketName
     * @return
     */
    String createBucket(String bucketName);


    /**
     * 删除桶
     *
     * @param bucketName
     */
    void removeBucket(String bucketName);

    /**
     * 获取桶列表
     *
     * @return
     */
    List listbuckets();


    /**
     * 获取某个桶中存储文件的列表
     *
     * @param bucket_name
     * @return
     */
    List listObject(String bucket_name);


    /**
     * 删除桶中的文件
     *
     * @param bucket_name
     * @param objectName
     */
    void deleteObject(String bucket_name,String objectName);
}

AwsServiceImpl.java

import com.amazonaws.services.s3.model.*;
import com.ecio.common.config.AwsConfig;
import com.ecio.common.utils.StringUtils;
import com.ecio.system.service.AwsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class AwsServiceImpl implements AwsService {

   @Value("${aws.accessKey}")
    private String accessKey;

    @Value("${aws.secretKey}")
    private String secretKey;

    @Value("${aws.region}")
    private String region;

    @Value("${aws.hostName}")
    private String hostName;

    @Value("${aws.baseBucketName}")
    private String bucketName;

    @Value("${aws.isLoadHostName}")
    private Boolean isLoadHostName;

    @Autowired
    private AwsConfig s3;

    @Override
    public String uploadFile(File file, String uploadPath) {
        try {
            if (file == null) {
                return null;
            }
            // 设置文件目录
            if (StringUtils.isNotEmpty(uploadPath)) {
                uploadPath = "/".equals(uploadPath.substring(uploadPath.length() - 1)) ? uploadPath : uploadPath + "/";
            } else {
                uploadPath = "default/";
            }
            // 生成随机文件名
            String expandedName = file.getName().substring(file.getName().lastIndexOf("."));

            String fileName = uploadPath + UUID.randomUUID().toString() + expandedName;
            // 设置文件上传对象
            PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file);
            // 设置公共读取
            request.withCannedAcl(CannedAccessControlList.PublicRead);
            // 上传文件
            PutObjectResult putObjectResult = s3.getAmazonS3().putObject(request);

            String result = hostName + File.separator + bucketName + File.separator + fileName;
            log.info("上传文件到桶成功,地址信息:{}", result);
            if (null != putObjectResult) {
                return result;
            }
        } catch (Exception e) {
            log.error("上传文件到桶失败:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String uploadMultipartFile(MultipartFile multipartFile, String uploadPath) {
        try {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(multipartFile.getContentType());
            objectMetadata.setContentLength(multipartFile.getSize());

            String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            log.info("suffix:{}",suffix);
            String simpleUUID = UUID.randomUUID().toString();
            log.info("simpleUUID:{}",simpleUUID);
            String fileName = simpleUUID + suffix;

            // 这里每一个分隔符表示一个图片
            String key = uploadPath + File.separator + File.separator +  fileName;
            log.info("fileName:{}",fileName);
            log.info("S3:{}",s3);
            log.info("bucketName:{}",bucketName);
            log.info("hostName:{}",hostName);
            PutObjectResult putObjectResult = s3.getAmazonS3().putObject(new PutObjectRequest(bucketName, key, multipartFile.getInputStream(), objectMetadata));
            String result = hostName + File.separator + bucketName + File.separator + uploadPath + File.separator +  fileName;
            log.info("Upload files to the bucket,address info:{}", result);
            if (null != putObjectResult) {
                return result;
            }
        } catch (Exception e) {
            log.error("Upload files to the bucket,Failed:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String downloadFile(String url) {
        try {
            if (StringUtils.isEmpty(url)) {
                return null;
            }
            GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, url);
            //设置过期时间
            httpRequest.setExpiration(expirationDate);
            return s3.getAmazonS3().generatePresignedUrl(httpRequest).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
	@Override
	public  void amazonS3Downloading(String key,String targetFilePath) {

        S3Object object = s3.getAmazonS3().getObject(new GetObjectRequest(bucketName, key));
        if (object != null) {
            log.info("Content-Type: " + object.getObjectMetadata().getContentType());
            InputStream input = null;
            FileOutputStream fileOutputStream = null;
            byte[] data = null;
            try {
                //获取文件流
                input = object.getObjectContent();
                data = new byte[input.available()];
                int len = 0;
                fileOutputStream = new FileOutputStream(targetFilePath);
                while ((len = input.read(data)) != -1) {
                    fileOutputStream.write(data, 0, len);
                }
                log.info("Downloading the file succeeded");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public String createBucket(String bucketName) {
        Bucket resp = s3.getAmazonS3().createBucket(bucketName);
        String bucket = resp.getName();
        return bucket;
    }

    @Override
    public void removeBucket(String bucketName) {
        s3.getAmazonS3().deleteBucket(bucketName);
    }

    @Override
    public List listbuckets() {
        List<String> buckets = new ArrayList<String>();
        List<Bucket> resp = s3.getAmazonS3().listBuckets();
        for (int i = 0; i < resp.size(); i++) {
            String bucket = resp.get(i).getName();
            buckets.add(bucket);
        }
        return buckets;
    }

    @Override
    public List listObject(String bucket_name) {
        List <String> objects = new ArrayList<String>();
        ObjectListing resp = s3.getAmazonS3().listObjects(bucket_name);
        List  object_summary = resp.getObjectSummaries();

        for (int i =  0;i<object_summary.size();i++) {
            String object_name = object_summary.get(i).toString();
            objects.add(object_name);
        }
        return objects;
    }

    @Override
    public void deleteObject(String bucket_name, String objectName) {
        s3.getAmazonS3().deleteObject(bucket_name, objectName);
    }
}
public class UploadToS3Util {

    // 创建s3对象
    private static final S3Client s3Client = S3Client
            .builder()
            .region(Region.CN_NORTH_1) //桶所在的位置
            .credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(AwsConfig.ACCESS_KEY, AwsConfig.SECRET_KEY,"")))
            .build();

    /**
     * s3上传文件
     * @param uploadKey  bucket中的存储文件名
     * @param file 待上传的文件流
     */
    public static boolean uploadToS3(String uploadKey, byte[] file) {
        try {
            if (file == null) {
                return false;
            }
            //添加文件夹dev(文件夹其实就是一个前缀)
            String prefix = "dev/";
            String folderKey = prefix.concat(uploadKey);
            PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(AwsConfig.BUCKET_NAME).key(folderKey).build();
            s3Client.putObject(putObjectRequest, RequestBody.fromBytes(file));
            return true;
        }catch (S3Exception e){
            System.err.println(e.getMessage());
            return false;
        }
    }

    /**
     * s3文件预览
     * @param uploadKey
     */
    public static String DownloadFromS3(String uploadKey) {
        BASE64Encoder encoder = new BASE64Encoder();
        String prefix = "dev/";
        String key = prefix.concat(uploadKey);
        GetObjectRequest objectRequest = GetObjectRequest
                .builder()
                .key(key)
                .bucket(AwsConfig.BUCKET_NAME)
                .build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        // 转换成base64文件流,用于前端数据解析
        return encoder.encodeBuffer(data).trim();
    }


    /**
     * s3文件下载
     * @param uploadKey
     */
    public static void DownloadFromS3(HttpServletRequest request, HttpServletResponse response, String uploadKey) {
        String prefix = "dev/";
        // S3上存储的key
        String key = prefix.concat(uploadKey);  
        GetObjectRequest objectRequest = GetObjectRequest
                .builder()
                .key(key)
                .bucket(AwsConfig.BUCKET_NAME)
                .build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        InputStream inputStream = objectBytes.asInputStream();
        BufferedInputStream bufInputStream = new BufferedInputStream(inputStream);
        OutputStream out = null;
        try {
            // 文件名称
            String fileName = "XXXXXX";
            response.setContentType("application/force-download");
            response.setHeader("fileName", URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = bufInputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            response.flushBuffer();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * s3删除文件
     * @param uploadKey
     * @return
     */
    public static  boolean deleteFromS3(String uploadKey){
        try {
            String  key = "dev/" + uploadKey;
            DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(AwsConfig.BUCKET_NAME).key(key).build();
            DeleteObjectResponse deleteObjectResponse = s3Client.deleteObject(deleteObjectRequest);
            return true;
        }catch (S3Exception e){
            System.err.println(e.getMessage());
            return false;
        }
    }

    /**
     * 获取桶里所有的文件
     */
    public static JSONArray ListBucketObjects() {
        JSONArray jar = new JSONArray();
        try {
            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(AwsConfig.BUCKET_NAME)
                    .build();
            ListObjectsResponse res = s3Client.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                JSONObject job = new JSONObject();
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                job.put("name",myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                job.put("size",calKb(myValue.size())+"  KBs");
                System.out.print("\n The owner is " + myValue.owner());
                jar.add(job);
            }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            jar.add(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return jar;
    }
    //文件大小
    private static long calKb(Long val) {
        return val/1024;
    }
}
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AWS S3(Amazon Simple Storage Service)是亚马逊提供的一种高度可扩展的对象存储服务。S3允许用户以云存储方式存储和检索任意数量的数据。以下是关于AWS S3的一些重要特性: 1. 可扩展性:AWS S3提供了高度可扩展的存储解决方案,能够适应任意规模的需求。无论是存储几个G的个人文件还是处理TB级别的数据,S3都能够满足需求。 2. 安全性:AWS S3提供多层次的安全控制来保护数据的完整性和安全性。用户可以通过控制访问权限来限制对存储桶和对象的访问。此外,S3还提供了加密功能,可以对数据进行加密,确保数据在输和存储过程中的安全。 3. 可靠性:AWS S3采用了多副本存储和自动修复机制,确保数据的可用性和持久性。S3将数据存储在多个设备和多个区域,并且自动处理设备故障,以确保数据不会丢失。 4. 数据访问:通过AWS S3,用户可以轻松地在任何地方访问其存储的数据。S3提供了REST和SOAP接口,可以通过编程方式进行高效、低延迟的数据访问。此外,S3还提供了网页界面,方便用户直接通过浏览器进行数据管理和操作。 5. 成本效益:AWS S3采用按需计费模式,根据用户实际存储的数据量和数据输的流量进行计费。用户只需支付实际使用的存储空间和输流量,无需提前购买硬件设备或维护硬件设备,从而节约了成本。 总之,AWS S3是一种可靠、安全、高度可扩展的云存储解决方案。通过提供灵活的数据管理和访问方式,以及强大的安全控制和可靠性,S3帮助用户轻松地存储和管理各种类型的数据,并实现数据的安全性和可用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值