S3Config.java 1.23 KB
package com.ruoyi.web.controller.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
public class S3Config {
    @Value("${aws.access-key}")
    private String accessKey;

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



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

    @Bean
    public S3Client s3Client() {
        // 创建 AwsBasicCredentials 实例,用于存储 AWS 的访问密钥和秘密访问密钥
        AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
        return S3Client.builder()
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .region(Region.CN_NORTH_1)
                .endpointOverride(java.net.URI.create(endpoint))
                .build();   // 创建 S3Client 实例
    }



}