Spring boot配置文件类型及阿里云OSS配置

Spring boot 常用配置文件

@value注解:用于外部配置的属性注入,具体用法为:@Value(“${配置文件中的key}”)

配置文件: application.properties

#阿里云OSS配置
aliyun.oss.endpoint=https://oss-cn-hangzhou.aliyuncs.com
aliyun.oss.accessKeyId=xxxxxx
aliyun.oss.accessKeySecret=xxxxxxx
aliyun.oss.bucketName=xxxxx

java类

@Component
public class AliOSSUtils {

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

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

}
spring boot 提供不同种类的配置文件
  • application.properties

    server.port=8080
    server.adress=127.0.0.1
    
  • application.yml/application.yaml // 推荐,清晰简洁

    server:
      port: 8080
      adress: 127.0.0.1
    
  • application.xml // spring boot不支持

    <server>
    	<port>8080</port>
        <adress>127.0.0.1</adress>
    </server>
    
yml
  • 大小写敏感
  • 数值前必须有空格作为分隔符
  • 使用缩进表示层级关系,缩进时只允许使用空格
  • 缩进的空格数目不重要,只要相同层级的元素左对齐即可
  • #表示注释

yml常用两类数据格式

# 定义对象/Map集合
user:
  name: Tom
  age: 20
  adress: beijing

# 定义数组/List/Set集合
hobby:
  - java
  - C
  - C++
  - python
yml配置文件
spring:
# 数据库连接信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: xxxx
    username: xxxx
    password: xxxxx
# 文件上传配置
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB
# Mybatis配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
# 阿里云配置
aliyun:
  oss:
    endpoint: xxxxxx
    accessKeyId: xxxxxxx
    accessKeySecret: xxxxxxxx
    bucketName: xxx

阿里云 OSS 工具

@ConfigurationProperties注解
@ConfigurationProperties注解
可以批量将外部的属性配置注入到bean对象的属性中
@Value注解只能一个一个的进行
@Component
@Data
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOssProperties {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}
/**
 * 阿里云 OSS 工具类
 */
@Component
public class AliOSSUtils {

    @Autowired
    private AliOssProperties aliOssProperties;
//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//
//    @Value("${aliyun.oss.accessKeyId}")
//    private String accessKeyId;
//
//    @Value("${aliyun.oss.accessKeySecret}")
//    private String accessKeySecret;
//
//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName;

    /**
     * 实现上传图片到OSS
     */
    public String upload(MultipartFile file) throws IOException {
        String accessKeyId =  aliOssProperties.getAccessKeyId();
        String accessKeySecret = aliOssProperties.getAccessKeySecret();
        String endpoint = aliOssProperties.getEndpoint();
        String bucketName = aliOssProperties.getBucketName();
        // 获取上传的文件的输入流
        InputStream inputStream = file.getInputStream();

        // 避免文件覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //上传文件到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //文件访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        // 关闭ossClient
        ossClient.shutdown();
        return url;// 把上传到oss的路径返回
    }
}

阿里云配置

# 阿里云配置
aliyun:
  oss:
    endpoint: xxxxxx
    accessKeyId: xxxxxxx
    accessKeySecret: xxxxxxxx
    bucketName: xxx
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将文件上传到阿里云对象存储(OSS)中的Spring Boot,你可以按照以下步骤进行操作: 1. 首先,确保你已经在阿里云上创建了一个OSS存储空间,并获得了访问密钥ID和访问密钥秘钥。 2. 在Spring Boot项目中添加阿里云OSS SDK的依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.13.1</version> </dependency> ``` 3. 在Spring Boot配置文件(application.properties或application.yml)中添加阿里云OSS的相关配置,包括访问密钥ID、访问密钥秘钥、存储空间名称等信息。 4. 创建一个用于上传文件的Service类,其中包含一个方法用于处理文件上传逻辑。你可以使用阿里云OSS SDK提供的`OSSClient`类来实现上传功能。以下是一个简单的示例: ```java @Service public class FileUploadService { @Value("${oss.accessKeyId}") private String accessKeyId; @Value("${oss.accessKeySecret}") private String accessKeySecret; @Value("${oss.endpoint}") private String endpoint; @Value("${oss.bucketName}") private String bucketName; public void uploadFile(MultipartFile file) { try { // 创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 获取文件名 String fileName = file.getOriginalFilename(); // 生成唯一的文件名,避免重复 String uniqueFileName = UUID.randomUUID().toString() + "_" + fileName; // 设置文件上传路径 String uploadPath = "your-upload-path/" + uniqueFileName; // 上传文件到OSS存储空间 ossClient.putObject(bucketName, uploadPath, file.getInputStream()); // 关闭OSSClient ossClient.shutdown(); } catch (IOException e) { e.printStackTrace(); // 处理异常 } } } ``` 在上述示例中,通过读取配置文件中的阿里云OSS相关配置,创建了一个`OSSClient`实例,并使用`putObject`方法将文件上传到指定的存储空间和路径中。 请记住,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。另外,还需要处理异常和错误情况。 希望以上信息能够对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值