springboot整合七牛云对象存储

目录

一、测试

二、整合


一、测试

注册七牛云账号,并进行邮箱绑定和实名认证。

七牛云每个月送10G完全够我们开发

创建一个空间

存储区域哪里离你近选哪里,访问控制一定要公开。

创建完成后,后期上传的静态资源,可以根据域名+文件名直接访问,自定义域名必须是备案域名

默认送一个测试域名,在上图位置

找到秘钥管理

AK,SK是我们访问要用到的

 在文档中找到开发者中心

复制依赖并导入

在测试类编写新方法

//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region0());
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释

UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
//如果是Windows情况下,格式是 D:\\qiniu\\test.png
String localFilePath = "/home/qiniu/test.png";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;

Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);

try {
    Response response = uploadManager.put(localFilePath, key, upToken);
    //解析上传成功的结果
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    System.out.println(putRet.key);
    System.out.println(putRet.hash);
} catch (QiniuException ex) {
    Response r = ex.response;
    System.err.println(r.toString());
    try {
        System.err.println(r.bodyString());
    } catch (QiniuException ex2) {
        //ignore
    }
}

 填入AK,SK,和你的空间名即可运行测试

如果遇到如下报错

{ResponseInfo:com.qiniu.http.Response@ba4f370,status:400, reqId:XhcAAACBZ4c1k18X, xlog:X-Log, xvia:, adress:upload.qiniup.com/xxx.xxx.xxx.xxx:xxx, duration:0.449000 s, error:incorrect region, please use up-z2.qiniup.com, bucket is: xxx}

这是因为区域不一样,更改配置信息

        Configuration cfg = new Configuration(Zone.zone2());

 Zone.zoneX()看please use up-zX是多少。

运行成功后可以看到存储区域多出了文件

打开外链可以访问(域名+文件名)

二、整合

导入依赖

    <!--七牛云上传-->
    <dependency>
      <groupId>com.qiniu</groupId>
      <artifactId>qiniu-java-sdk</artifactId>
      <version>7.2.11</version>
    </dependency>
 
    <!-- 七牛云依赖Gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
    </dependency>

配置yml

oss:
  qiniu:
    domain: qtxxxxxxxx.hn-xxx.xxxxx.com # 访问域名(默认使用七牛云测试域名)
    accessKey: Gn0uwxxxxxxxxxxxxxxxxxxxxy3GEVmZqR58ed # 公钥 刚才的AK
    secretKey: hs-ScVOxxxxxxxxxxxo0yG33uHm8_NkmnKy # 私钥 刚才的SK
    bucketName: officxxxxxxxxxxicture  #存储空间名称

编写配置类

package com.qingyun.gulimall.product.config;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Data
@Component
public class QiNiuYunConfig {
    /**
     * 七牛域名domain
     */
    @Value("${oss.qiniu.domain}")
    private String qiniuDomain;
    /**
     * 七牛ACCESS_KEY
     */
    @Value("${oss.qiniu.accessKey}")
    private String qiniuAccessKey;
    /**
     * 七牛SECRET_KEY
     */
    @Value("${oss.qiniu.secretKey}")
    private String qiniuSecretKey;
    /**
     * 七牛空间名
     */
    @Value("${oss.qiniu.bucketName}")
    private String qiniuBucketName;
}

后续继续编写Service,实现类,Controller即可正常使用

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现Spring Boot整合七牛云上传文件,可以按照以下步骤进行: 1.引入七牛云Java SDK 在pom.xml中引入七牛云Java SDK: ```xml <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.2.0,)</version> </dependency> ``` 2.配置七牛云参数 在application.yml或application.properties中添加七牛云的参数: ```yml qiniu: accessKey: your_access_key secretKey: your_secret_key bucket: your_bucket_name domain: your_domain_name ``` 3.编写上传文件的代码 在需要上传文件的地方编写上传文件的代码,示例代码如下: ```java @Service public class QiniuService { @Autowired private QiniuConfig qiniuConfig; /** * 上传文件到七牛云 * * @param file 文件对象 * @return 文件访问URL */ public String uploadFile(File file) throws QiniuException { // 构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Zone.autoZone()); // ...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); // 生成上传凭证,然后准备上传 String accessKey = qiniuConfig.getAccessKey(); String secretKey = qiniuConfig.getSecretKey(); String bucket = qiniuConfig.getBucket(); // 如果是Windows情况下,格式是 D:\\qiniu\\test.png String localFilePath = file.getAbsolutePath(); // 默认不指定key的情况下,以文件内容的hash值作为文件名 String key = null; Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); try { Response response = uploadManager.put(localFilePath, key, upToken); // 解析上传成功的结果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); return qiniuConfig.getDomain() + "/" + putRet.key; } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { // ignore } throw ex; } } } ``` 4.测试上传文件 在测试类中编写测试上传文件的代码,示例代码如下: ```java @RunWith(SpringRunner.class) @SpringBootTest public class QiniuServiceTest { @Autowired private QiniuService qiniuService; @Test public void testUploadFile() throws QiniuException { File file = new File("test.png"); String url = qiniuService.uploadFile(file); System.out.println(url); } } ``` 其中,test.png是要上传的文件名。运行测试类即可上传文件到七牛云,并返回文件的访问URL。 以上就是Spring Boot整合七牛云上传文件的全部步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天辰尽落

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

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

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

打赏作者

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

抵扣说明:

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

余额充值