当前位置:   article > 正文

S3(亚马逊云)工具类及使用【java】_java s3工具类

java s3工具类

S3(亚马逊云)工具类及使用【java】


前言

提示:这里是本文要记录的大概内容:

以下是亚马逊云工具类使用说明,有问题请评论 我们一起探讨。


提示:以下是本篇文章正文内容,下面案例可供参考

FileServiceImpl

package com.adups.business.mgmt.service.file.impl;

import com.adups.business.mgmt.config.S3Config;
import com.adups.business.mgmt.service.file.FileService;
import com.adups.business.mgmt.utils.AmazonS3Manager;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private S3Config s3Config;


    /**
     * 上传文件
     *
     * @param uploadPath
     * @param uploadFile
     * @return
     */
    public String upload(String uploadPath, File uploadFile) {
        String filePathName = getFilePath(s3Config.getDirectory() + uploadPath, uploadFile);
        AmazonS3Manager.uploadToS3(s3Config.getBucketName(), uploadFile, filePathName, CannedAccessControlList.PublicReadWrite);
        return filePathName;
    }

    /**
     * 获取临时 URL
     *
     * @param pathUrl 包存储的路径,形如 xxxx/xxx/xx/x.zip
     * @return
     */
    @Override
    public String getUrl(String pathUrl) {
        String url = AmazonS3Manager.getUrlToS3(s3Config.getBucketName(), pathUrl);
        return url;
    }


    /**
     * @param path
     * @param file
     * @desc 生成路径以及文件名
     */
    @Override
    public String getFilePath(String path, File file) {
        /**获取文件后缀名**/
        String suffix = file.getName().substring(file.getName().lastIndexOf("."));
        /**上传的文件名称重命名**/
        String fileName = path + UUID.randomUUID() + "/" + file.getName();
        return fileName;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

AmazonS3Manager


package com.adups.business.mgmt.utils;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * @author DingYF
 * @date : 2022/8/19 14:54
 */
@Slf4j
public class AmazonS3Manager {
//    public static final Logger logger = LoggerFactory.getLogger(AmazonS3Manager.class);

    private static String AMAZON_ENDPOINT_URL;
    private static String AMAZON_AWS_ACCESS_KEY;
    private static String AMAZON_AWS_SECRET_KEY;


    static {

        try {

            AMAZON_ENDPOINT_URL = "https://jqecs.sgm.saic-gm.com:9021";
            AMAZON_AWS_ACCESS_KEY = "ismsqa";
            AMAZON_AWS_SECRET_KEY = "Qj4471I7F/0W2T1CenJObP85zaaauDYsMR5imlgS";
        } catch (Exception e) {
            log.error("amazonS3文件服务器基础参数加载出错" + e.getMessage());
        }
        log.info("amazonS3 文件服务器基础参数加载完成 access_key=" + AMAZON_AWS_ACCESS_KEY + " endpoint_url=" + AMAZON_ENDPOINT_URL);
    }


    /**
     * 初始化连接,每次使用需要重新连接
     */
    public static AmazonS3 initAmazonS3() {

        AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(AMAZON_AWS_ACCESS_KEY, AMAZON_AWS_SECRET_KEY));
        s3.setRegion(Region.getRegion(Regions.CN_NORTH_1));
        s3.setEndpoint(AMAZON_ENDPOINT_URL);
        s3.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        return s3;
    }

    /**
     * 上传文件
     *
     * @param bucketName     桶名
     * @param tempFile       待上传文件
     * @param remoteFileName 文件名
     * @return
     */
    public static boolean uploadToS3(String bucketName, File tempFile, String remoteFileName, CannedAccessControlList fileType) {

        try {
            AmazonS3 s3 = initAmazonS3();

            if (!s3.doesBucketExistV2(bucketName)) {
                s3.createBucket(bucketName);
            }
            s3.putObject(new PutObjectRequest(bucketName, remoteFileName, tempFile).withCannedAcl(fileType));
            return true;
        } catch (Exception ase) {
            log.error("amazonS3上传文件File模式异常 " + ase.getMessage(), ase);
        }
        return false;
    }

    /**
     * 上传文件
     *
     * @param bucketName     桶名
     * @param multipartFile  待上传文件
     * @param remoteFileName 文件名
     * @return
     */
    public static boolean uploadToS3(String bucketName, CommonsMultipartFile multipartFile, String remoteFileName, CannedAccessControlList fileType) {

        InputStream in = null;
        try {
            AmazonS3 s3 = initAmazonS3();
            in = multipartFile.getInputStream();
            FileItem fileItem = multipartFile.getFileItem();

            if (!s3.doesBucketExistV2(bucketName)) {
                s3.createBucket(bucketName);
            }
            ObjectMetadata omd = new ObjectMetadata();
            omd.setContentType(fileItem.getContentType());
            omd.setContentLength(fileItem.getSize());
            omd.setHeader("filename", fileItem.getName());

            s3.putObject(new PutObjectRequest(bucketName, remoteFileName, in, omd).withCannedAcl(fileType));
            return true;

        } catch (Exception ase) {
            log.error("amazonS3上传文件InputStream模式异常 " + ase.getMessage(), ase);

        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error("amazonS3上传文件 关闭InputStream流异常 " + e.getMessage(), e);
                }
            }
        }
        return false;
    }


    /**
     * 下载文件
     *
     * @param bucketName     桶名
     * @param remoteFileName 文件名
     * @param path           下载路径
     */
    public static boolean downFromS3(String bucketName, String remoteFileName, String path) {
        try {
            AmazonS3 s3 = initAmazonS3();

            GetObjectRequest request = new GetObjectRequest(bucketName, remoteFileName);
            ObjectMetadata metadata = s3.getObject(request, new File(path));
            return true;
        } catch (Exception ase) {
            log.error("amazonS3下载文件异常 " + ase.getMessage(), ase);
        }
        return false;
    }


    /**
     * 删除文件
     *
     * @param bucketName     桶名
     * @param remoteFileName 待删除文件名
     * @throws IOException
     */
    public static void delFromS3(String bucketName, String remoteFileName) {
        try {
            AmazonS3 s3 = initAmazonS3();
            s3.deleteObject(bucketName, remoteFileName);
        } catch (Exception ase) {
            log.error("amazonS3删除文件异常 " + ase.getMessage(), ase);
        }
    }


    /**
     * 获取短链接 带过期时间
     *
     * @param bucketName     桶名称
     * @param remoteFileName 文件名称
     * @param expiration     过期时间/秒
     */
    public static String getUrlFromS3(String bucketName, String remoteFileName, int expiration) {
        try {
            AmazonS3 s3 = initAmazonS3();
            GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
            httpRequest.setExpiration(new Date(new Date().getTime() + expiration * 1000));

            URL url = s3.generatePresignedUrl(httpRequest);
            return String.valueOf(url);
        } catch (Exception e) {
            log.error("amazonS3获取临时链接异常 " + e.getMessage(), e);
        }
        return null;
    }

    /**
     * 获取永久链接
     *
     * @param bucketName     桶名称
     * @param remoteFileName 文件名称
     */
    public static String getUrlFromS3(String bucketName, String remoteFileName) {

        String url = "";
        try {
            AmazonS3 s3 = initAmazonS3();
            GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
            url = String.valueOf(s3.generatePresignedUrl(urlRequest)).split("\\?")[0];
            if (url.indexOf(remoteFileName) == -1) {
                throw new RuntimeException("url文件名称校验不合法");
            }
            return url;

        } catch (Exception e) {
            log.error("amazonS3获取永久链接异常 " + e.getMessage(), e);
            return "";
        }
    }

    /**
     * 获取永久链接-包含证书
     *
     * @param bucketName     桶名称
     * @param remoteFileName 文件名称
     */
    public static String getUrlToS3(String bucketName, String remoteFileName) {
        try {
            AmazonS3 s3 = initAmazonS3();
            GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
            URL url = s3.generatePresignedUrl(urlRequest);
            return String.valueOf(url);

        } catch (Exception e) {
            log.error("amazonS3获取永久链接异常 " + e.getMessage(), e);
            return null;
        }
    }

    public static AmazonS3 getS3() {
        return initAmazonS3();
    }

    /**
     * 根据桶名称获取文件集合
     */
    public static List<S3ObjectSummary> getFileMsgByBucketName(String bucketName) {
        AmazonS3 s3 = initAmazonS3();
        ObjectListing objectListing = s3.listObjects(bucketName);
        List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        return objectSummaries;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244

S3Config

package com.adups.business.mgmt.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * @author yuzhengwei
 */
@Configuration
@ConfigurationProperties(prefix = "s3")
@Data
@Lazy
public class S3Config {
    private String directory;
    private String awsAccessKey;
    private String awsSecretKey;
    private String bucketName;
    private String endpoint;
    private String signingRegion;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

配置yml

#S3配置
s3:
  awsAccessKey: ismsqa
  awsSecretKey: Qj4471I7F/0W2T1CenJObP85zaaauDYsMR5imlgS
  bucketName: isms
  endpoint: https://jqecs.sgm.saic-gm.com:9021
  signingRegion: cn-northwest-1
  directory: rds/
  enable: true
  
#临时存放路径
  util:
    s3FilePath: /data/rds/s3/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

maven包


	        <!-- AWS SDK start -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.415</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-core</artifactId>
            <version>1.11.415</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-kms</artifactId>
            <version>1.11.415</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>jmespath-java</artifactId>
            <version>1.11.415</version>
        </dependency>
        <!-- AWS SDK end -->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

s3配置与使用

链接: link.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/285748
推荐阅读
相关标签
  

闽ICP备14008679号