Springboot:华为云obs文件上传和删除

不讲别的,直接看代码

准备

AccessKey:访问密钥
SecretKey:访问密钥
endPoint:终端节点
bucketName:桶

1 添加maven依赖

<dependency>
    <groupId>com.huaweicloud</groupId>
    <artifactId>esdk-obs-java</artifactId>
    <version>3.19.7</version>
</dependency>

2 添加obs的配置信息

# huawei ods
huawei.obs.ak=hajkfhdsajkhjdkahsfjs
huawei.obs.sk=hjaksjfhdakdfhajkfhdsajkhjdkahsfjsadhaljkhdfsd
# 上传的endPoint
huawei.obs.upload.endPoint=endPoint.com
# 访问的endPoint
huawei.obs.access.endPoint=https://endPoint.com
# 桶
huawei.obs.bucketName=obs-bucket-dev

3 配置obs

import com.obs.services.ObsClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObsConfig {

    @Value("${huawei.obs.ak}")
    private String ak;

    @Value("${huawei.obs.sk}")
    private String sk;

    @Value("${huawei.obs.upload.endPoint}")
    private String endPoint;

    @Bean
    public ObsClient getObsClient(){
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        return obsClient;
    }
}

4 创建obs工具类,提供上传和删除功能

import com.obs.services.ObsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Component
public class ObsUtil {

    @Autowired
    private ObsClient obsClient;

    @Value("${huawei.obs.bucketName}")
    private String bucketName;

    //上传文件,multipartFile就是你要的文件,
    //objectKey就是文件名,如果桶中有文件夹的话,如往test文件上传test.txt文件,那么objectKey就是test/test.txt
    public void uploadFile(MultipartFile multipartFile, String objectKey) throws Exception{
        InputStream inputStream = multipartFile.getInputStream();
        obsClient.putObject(bucketName, objectKey, inputStream);
        inputStream.close();
        obsClient.close();
    }
    public void deleteFile(String objectKey) throws Exception{
        obsClient.deleteObject(bucketName, objectKey);
        obsClient.close();
    }
}

效果展示

在这里插入图片描述

你可以使用 Spring Boot 华为云 OBS SDK 来上传 MultipartFile 对象到华为云 OBS。下面是一个示例代码: 1. 首先,你需要在 pom.xml 文件中添加华为云 OBS 的依赖: ```xml <dependency> <groupId>com.obs</groupId> <artifactId>obs-sdk-java</artifactId> <version>3.20.0</version> </dependency> ``` 2. 然后,你需要配置华为云 OBS 的相关信息,如 accessKey、secretKey、endpoint 等。你可以将这些信息放在 application.properties 或 application.yml 文件中,如: ```yaml huawei: obs: accessKey: yourAccessKey secretKey: yourSecretKey endpoint: yourEndpoint bucketName: yourBucketName ``` 3. 接下来,创建一个 Service 类来处理文件上传的逻辑: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.obs.services.ObsClient; import com.obs.services.model.PutObjectRequest; import java.io.IOException; @Service public class ObsService { @Value("${huawei.obs.accessKey}") private String accessKey; @Value("${huawei.obs.secretKey}") private String secretKey; @Value("${huawei.obs.endpoint}") private String endpoint; @Value("${huawei.obs.bucketName}") private String bucketName; public void uploadFile(MultipartFile file) throws IOException { ObsClient obsClient = new ObsClient(accessKey, secretKey, endpoint); PutObjectRequest request = new PutObjectRequest(bucketName, file.getOriginalFilename(), file.getInputStream()); obsClient.putObject(request); obsClient.close(); } } ``` 4. 最后,在你的 Controller 中使用 ObsService 类来处理文件上传请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @RestController public class FileUploadController { @Autowired private ObsService obsService; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { try { obsService.uploadFile(file); return "File uploaded successfully."; } catch (IOException e) { return "Failed to upload file."; } } } ``` 以上就是使用 Spring Boot 华为云 OBS SDK 上传 MultipartFile 的示例代码。确保你已经正确配置了华为云 OBS 的相关信息,并根据你的实际情况进行修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高并发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值