项目实战----图片上传oss服务器

依赖

       <dependency>
          <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
          <version>3.10.2</version>
        </dependency>

配置yml

aliyun:
  oss:
    bucket_diasiahhr_name: diasiatest
    bucket_diasiaparty_name: diasiatest
    bucket_diasiavideo_name: diasiatest
    bucket_diasiameeting: diasiatest
    bucket_name: diasiatest
    access_key: -----------------------
    access_secret: --------------------
    endpoint: -----------
    filedir: partner/
    expire_year: 30
    sts_access_key: ------------
    sts_access_secret: -----------
    sts_endpoint: -------------
    sts_filedir: video/
    sts_arn: ------------

自己的/自己公司的 代表 “--------------------------”

OssConfig

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * OSS存储配置
 */
@Configuration
@Data
@Component
public class OssConfig {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.access_key}")
    private String accessKeyId;
    @Value("${aliyun.oss.access_secret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.filedir}")
    private String filedir;
    @Value("${aliyun.oss.expire_year}")
    private int expireYear;
    @Value("${aliyun.oss.sts_endpoint}")
    private String stsEndpoint;
    @Value("${aliyun.oss.sts_access_key}")
    private String stsAccessKeyId;
    @Value("${aliyun.oss.sts_access_secret}")
    private String stsAccessKeySecret;
    @Value("${aliyun.oss.sts_filedir}")
    private String stsFiledir;
    @Value("${aliyun.oss.sts_arn}")
    private String stsArn;

    @Value("${aliyun.oss.bucket_diasiahhr_name}")
    private String bucketDiasiahhrName;
    @Value("${aliyun.oss.bucket_diasiaparty_name}")
    private String bucketDiasiapartyName;
    @Value("${aliyun.oss.bucket_diasiavideo_name}")
    private String bucketDiasiavideoName;
    @Value("${aliyun.oss.bucket_diasiameeting}")
    private String bucketDiasiaMeeting;

    @Bean
    public OssClientFactoryBean ossClientFactoryBean() {
        final OssClientFactoryBean factoryBean = new OssClientFactoryBean();
        factoryBean.setEndpoint(endpoint);
        factoryBean.setAccessKeyId(accessKeyId);
        factoryBean.setAccessKeySecret(accessKeySecret);
        return factoryBean;
    }
}

OssClientFactoryBean

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

@Data
public class OssClientFactoryBean implements FactoryBean<OSS>, InitializingBean, DisposableBean {

    private OSS oss;
    private String bucketName;
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;

    @Override
    public OSS getObject() throws Exception {
        return this.oss;
    }

    @Override
    public Class<?> getObjectType() {
        return OSS.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void destroy() throws Exception {
        if (this.oss != null) {
            this.oss.shutdown();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.endpoint, "'endpoint' must be not null");
        Assert.notNull(this.accessKeyId, "'accessKeyId' must be not null");
        Assert.notNull(this.accessKeySecret, "'accessKeySecret' must be not null");
        this.oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

}

TestController

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.studydemo.ossdemo.util.OssConfig;
import lombok.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import result.CommonResult;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@Controller
public class TestController {
    @Resource
    @Getter
    private OssConfig ossConfig;


    //单图片上传
    @RequestMapping(value = "/createPlay", method = RequestMethod.GET)
    public CommonResult<Void> uploadImg(
            @RequestParam String name,
            @RequestParam String startData,
            @RequestParam String dataHour,
            @RequestPart("file") MultipartFile multipartFile
    ) throws IOException {
        String time = startData + " " + dataHour;

        OSSClient ossClient=new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        String bucket=ossConfig.getBucketDiasiaMeeting();
        // 上传文件流。
        InputStream inputStream = multipartFile.getInputStream();
        String[] filenames = StringUtils.split(multipartFile.getOriginalFilename(), ".");
        String objectId = IdUtil.objectId();
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(inputStream.available());
        metadata.setCacheControl("no-cache");
        metadata.setHeader("Pragma", "no-cache");
        metadata.setContentType("image/jpg");
        String fileName = objectId + "." + filenames[1];
        metadata.setContentDisposition("inline;filename=" + fileName);
        ossClient.putObject(bucket, "appPlayPicture/" + objectId + "." + filenames[1], inputStream, metadata);
        Date expir = new Date(System.currentTimeMillis() + 36000000 * 10000000);
        String url = ossClient.generatePresignedUrl(bucket, "appPlayPicture/" + fileName, expir).toString();
        System.out.println(url);
        //业务添加-------传个url添加到数据库即可,前端获取--查询
        //partnerAppVideoService.creatPlay(name,startData,dataHour,url);
        return CommonResult.success();
    }


}

成功!单图片上传-----
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值