当前位置:   article > 正文

Java利用aws对s3的操作_java使用aws 使用s3教程

java使用aws 使用s3教程

1.配置S3信息并进行连接

package com.demo.common.utils.aws;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName:
 * @Description: 链接s3
 * @Author: Wzp
 * @Date: 2022/3/30
 * @Version 1.0
 **/
@Configuration
public class AwsConfig {
    @Value("${s3.awsAccessKey}")
    private String awsAccessKey;
    @Value("${s3.awsSecretKey}")
    private String awsSecretKey;

    @Bean
    public AmazonS3 amazonS3() {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withRegion(Regions.AP_SOUTHEAST_1).withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
        return amazonS3;
    }
}

  • 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

2.对s3桶的基本操作

package com.demo.common.utils.aws;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.model.analytics.AnalyticsConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Description: s3桶常用操作
 * @Author: Wzp
 * @Date: 2022/3/29
 * @Version 1.0
 **/
@Component
public class AwsS3Bucket {

    @Autowired
    public AmazonS3 s3;

    /**
     * s3桶名称
     */
    @Value("${s3.bucketName}")
    private  String bucketName;
    /**
     * s3区域
     */
    private String region = Regions.AP_SOUTHEAST_1.getName();

    /**
     * 列出所有的s3桶
     */
    public void listBuckets() {
        List<Bucket> buckets = s3.listBuckets();
        buckets.forEach(item -> {
            System.out.println(item.toString());
        });
    }

    /**
     * 创建一个s3桶-默认
     */
    public void createBucketBase() {
        Bucket bucket = s3.createBucket(bucketName);
        System.out.println(bucket.toString());
    }

    /**
     * 创建一个s3桶-带参数
     */
    public void createBucketWithParams() {
        //指定名称和区域
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName, region);
        //是否启用对象锁-启用后,阻止删除对象
        createBucketRequest.setObjectLockEnabledForBucket(true);
        Bucket bucket = s3.createBucket(createBucketRequest);
        System.out.println(bucket.toString());
    }

    /**
     * 删除一个s3桶
     */
    public void deleteBucket() {
        DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucketName);
        s3.deleteBucket(deleteBucketRequest);
        System.out.println("delete bucket success");
    }

    /**
     * s3桶配置
     */
    public void configBucket() {
        /**
         * 加速配置
         */
        BucketAccelerateConfiguration bucketAccelerateConfiguration = new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled);
        s3.setBucketAccelerateConfiguration(bucketName, bucketAccelerateConfiguration);
        /**
         * 权限配置
         */
        //公共访问权限
        SetBucketAclRequest setBucketAclRequest = new SetBucketAclRequest(bucketName, CannedAccessControlList.Private);
        s3.setBucketAcl(setBucketAclRequest);
        //访问控制列表
        AccessControlList accessControlList = new AccessControlList();
        accessControlList.setRequesterCharged(true);
        accessControlList.setOwner(null);
        SetBucketAclRequest setBucketAclRequest2 = new SetBucketAclRequest(bucketName, accessControlList);
        s3.setBucketAcl(setBucketAclRequest2);
        /**
         * 分析配置
         */
        AnalyticsConfiguration analyticsConfiguration = new AnalyticsConfiguration();
        analyticsConfiguration.setId(null);
        SetBucketAnalyticsConfigurationRequest setBucketAnalyticsConfigurationRequest = new SetBucketAnalyticsConfigurationRequest(bucketName, analyticsConfiguration);
        s3.setBucketAnalyticsConfiguration(setBucketAnalyticsConfigurationRequest);
        /**
         * 生命周期配置
         */
        BucketLifecycleConfiguration bucketLifecycleConfiguration = new BucketLifecycleConfiguration();
        List<BucketLifecycleConfiguration.Rule> rules = new ArrayList();
        //需要预先制定规则
        BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule().withId(null);
        rules.add(rule);
        bucketLifecycleConfiguration.setRules(rules);
        SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest = new SetBucketLifecycleConfigurationRequest(bucketName, bucketLifecycleConfiguration);
        s3.setBucketLifecycleConfiguration(setBucketLifecycleConfigurationRequest);
        /**
         * 加密配置
         * 当对象存储在s3时默认是加密的
         */
        SetBucketEncryptionRequest setBucketEncryptionRequest = new SetBucketEncryptionRequest();
        setBucketEncryptionRequest.setBucketName(bucketName);
        ServerSideEncryptionConfiguration serverSideEncryptionConfiguration = new ServerSideEncryptionConfiguration();
        //同样,需要预先制定规则
        serverSideEncryptionConfiguration.setRules(null);
        setBucketEncryptionRequest.setServerSideEncryptionConfiguration(serverSideEncryptionConfiguration);
        s3.setBucketEncryption(setBucketEncryptionRequest);
        /**
         * 版本控制配置
         */
        BucketVersioningConfiguration bucketVersioningConfiguration = new BucketVersioningConfiguration();
        bucketVersioningConfiguration.setMfaDeleteEnabled(true);
        bucketVersioningConfiguration.setStatus(BucketVersioningConfiguration.ENABLED);
        SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = new SetBucketVersioningConfigurationRequest(bucketName, bucketVersioningConfiguration);
        s3.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);

        /**
         * 为s3指定一个策略-s3的策略是唯一的
         */
        s3.setBucketPolicy(null);
        /**
         * 日志记录配置
         */
        s3.setBucketLoggingConfiguration(null);
        /**
         * 通知配置
         */
        s3.setBucketNotificationConfiguration(null);
        /**
         * 复制配置
         */
        s3.setBucketReplicationConfiguration(null);
        /**
         * 标签配置
         */
        s3.setBucketTaggingConfiguration(null);
        /**
         * 静态网站托管配置
         */
        s3.setBucketWebsiteConfiguration(null);
        /**
         * 指标配置
         */
        s3.setBucketMetricsConfiguration(null);

    }

}

  • 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

3.对S3桶中文件的基本操作

package com.demo.common.utils.aws;


import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.List;

/**
 * @ClassName:
 * @Description: s3桶文件的常用操作
 * @Author: Wzp
 * @Date: 2022/3/29
 * @Version 1.0
 **/
@Slf4j
@Component
public class AwsS3File {
    @Autowired
    private AmazonS3 s3;

    /**
     * s3桶名称
     */
    @Value("${s3.bucketName}")
    private String bucketName;
    /**
     * 上传到s3桶后的文件名,key如果是带路径的则会自动创建路径,格式为 文件夹名+/+文件名称
     */
    private String key = "test/test";
    /**
     * 本地文件
     */
    private String localFilePath = "/Users/yz/Documents/";
    /**
     * 列出一个s3桶内的文件
     */
    public void listFiles() {
        //可以加入文件名前缀当作搜索条件
        String prefix = "";
        ObjectListing objectListing = s3.listObjects(bucketName);
        List<S3ObjectSummary> s3ObjectSummaryList = objectListing.getObjectSummaries();
        s3ObjectSummaryList.forEach(item -> {
            //文件路径及名称-如果是文件夹,以"/"标识路径
            System.out.print(item.getKey() + ",");
            //文件UUID
            System.out.println(item.getETag());
        });
    }

    /**
     * 上传一个文件到s3桶
     * 注意1:如果key相同,则替换
     * 注意2:如果key是一个层次路径,那么s3会自动创建相应文件夹
     * @param file
     * @param content 指定存放的目录
     */
    public String uploadFile(File file, String content) {
        String filePath = file.getAbsolutePath();
        String key = content + "/" + filePath.substring(filePath.lastIndexOf("/") + 1,filePath.indexOf("."));
        log.info("上传文件到s3的请求参数为:file:{},content:{},key:{}",file,content,key);
        try{
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            //设置权限属性等-非必需
//        putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
            //上传
            PutObjectResult putObjectResult = s3.putObject(putObjectRequest);
            log.info("获取上传后文件路径地址");
            String fileUrl = getUrlFromS3(key);
            return fileUrl;
        }catch (Exception e){
            log.info("执行上传和获取文件链接异常:{}",e.getMessage());
        }
        return null;
    }
    /**
     * @param @param  remoteFileName 文件名
     * @param @return
     * @param @throws IOException    设定文件
     * @return String    返回类型
     * @throws
     * @Title: getUrlFromS3
     * @Description: 获取文件的url
     */
    public String getUrlFromS3(String key) throws IOException {
        try {
            GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, key);
            String url = s3.generatePresignedUrl(httpRequest).toString();//临时链接
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 从s3桶删除一个文件
     * 注意:如果删除的是最后一个文件,那么,上层文件夹也会被同时删除
     */
    public void deleteFile() {
        s3.deleteObject(bucketName, key);
    }

    /**
     * 获取一个文件
     *
     * @throws IOException
     */
    public S3Object downloadFile() throws IOException {
        //获取文件
        S3Object s3Object = s3.getObject(bucketName, key);
        //s3文件输入流,继承了FilterInputStream
        S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
        //文件属性等数据
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        //文件的key-可以用来获取文件名
        System.out.println(s3Object.getKey());
        //桶名
        System.out.println(s3Object.getBucketName());
        System.out.println(s3Object.getRedirectLocation());
        System.out.println(s3Object.getTaggingCount());
        return s3Object;
    }

    /**
     * 获取一个文件,并打印出来
     * @param content 目录
     * @param remoteFileName 需要下载的文件名
     * @throws IOException
     */
    public void downloadFileWithPrint(String content, String remoteFileName) throws IOException {
        String key = content + "/" + remoteFileName;
        S3Object s3Object = s3.getObject(bucketName, key);
        S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
        //如果是txt文件,可以把内容打印出来
        StringBuffer stringBuffer = new StringBuffer();
        byte[] buffer = new byte[1024];
        while ((s3ObjectInputStream.read(buffer)) != -1) {
            stringBuffer.append(new String(buffer, "UTF-8"));
        }
        s3ObjectInputStream.close();
        System.out.println(stringBuffer.toString().trim());
    }

    /**
     * 下载文件并保存到本地
     * @param content 目录
     * @param remoteFileName 需要下载的文件名
     * @throws IOException
     */
    public void downloadFileWithSave(String content, String remoteFileName) throws IOException {
        String key = content + "/" + remoteFileName;
        S3Object s3Object = s3.getObject(bucketName, key);
        S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
        OutputStream os = null;
        try {
            //文件名
            String fileName = s3Object.getKey().substring(s3Object.getKey().lastIndexOf("/"));
//            String fileName = s3Object.getKey();
            os = new FileOutputStream(new File(localFilePath + "s3/" + fileName));
            byte[] buffer = new byte[1024];
            while ((s3ObjectInputStream.read(buffer)) != -1) {
                os.write(buffer);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
            if (s3ObjectInputStream != null) {
                s3ObjectInputStream.close();
            }
        }
    }

    /**
     * 复制一个文件
     * 比较简单,就是复制,可以选择原文件的版本
     */
    public void copyFile() {
        String sourceBucketName = bucketName;
        String sourceKey = key;
        String sourceVersionId = null;
        String destinationBucketName = bucketName;
        String destinationKey = key;
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKey, sourceVersionId, destinationBucketName, destinationKey);
        CopyObjectResult copyObjectResult = s3.copyObject(copyObjectRequest);
        System.out.println(copyObjectResult.toString());
    }

}

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

闽ICP备14008679号