AWS S3 上传文件

application.properties  配置文件

s3.accessKey= ----
s3.secretKey= ----
s3.bucket.name= ----

1.引入依赖

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

 2.创建s3Client

package com.nft.util;

import com.amazonaws.ClientConfiguration;
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.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AwsS3Component implements InitializingBean {

    @Value("${s3.accessKey}")
    String accessKey;
    @Value("${s3.secretKey}")
    String secretKey;
    @Value("${s3.bucket.name}")
    String bucketName;

    @Value("${proxy.host}")
    String proxyHost;
    @Value("${proxy.port}")
    int proxyPort;

    public static AmazonS3 s3Client = null;


    @Override
    public void afterPropertiesSet() {
        Regions clientRegion = Regions.EU_CENTRAL_1;
        ClientConfiguration clientConfiguration = new ClientConfiguration();

        if (proxyHost != null) {
            clientConfiguration.setProxyHost(proxyHost);
            clientConfiguration.setProxyPort(proxyPort);
            clientConfiguration.setProxyUsername("");
            clientConfiguration.setProxyPassword("");
        }


        BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
        // us-west-2 is AWS Oregon
        if (proxyHost != null) {
            s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withClientConfiguration(clientConfiguration)
                    .withPathStyleAccessEnabled(true)
                    .build();
        }else{
            s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withPathStyleAccessEnabled(true)
                    .build();
        }
    }

}

3.调用工具进行上传

file---文件
folderName---s3系统文件名
bucketName---s3桶名称


AwsS3Component.s3Client.putObject(new PutObjectRequest(bucketName, folderName + "/" + filename, file.getInputStream(), objectMetadata)
                    .withCannedAcl(CannedAccessControlList.PublicRead));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值