功能实现:阿里云上传文件

部署运行你感兴趣的模型镜像

pom.xml

<!-- 阿里云oss依赖 -->
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.1.0</version>
</dependency>

application.yml

#OOS配置
aliyun:
  oss:
    endpoint: oss-cn-chengdu.aliyuncs.com
    accessKeyId: zzzzzzzzzzzzzzzzzzzzzzzzz
    secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    bucket: zxy-test

OssFileApi.java

package com.zxy.api;

import org.springframework.http.MediaType;
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.multipart.MultipartFile;

/**
 * 文件API
 */
public interface OssFileApi {

	/**
	 * 文件上传
	 * @param file 文件
	 * @return 文件保存地址
	 */
	@RequestMapping(value = "v1/mini/file/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	String upload(@RequestParam("file") MultipartFile file);

}

OssFileController.java

package com.zxy.controller;

import com.zxy.api.OssFileApi;
import com.zxy.service.OssFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传API实现
 */
@RestController
@RequiredArgsConstructor
public class OssFileController implements OssFileApi {

	private final OssFileService ossFileService;

	@Override
	public String upload(MultipartFile file) {
		return ossFileService.upload(file);
	}

}

OssFileService.java

package com.zxy.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * aliyun存储文件
 */
public interface OssFileService {

	/**
	 * 文件上传
	 * @param file 文件
	 * @return 文件保存地址
	 */
	String upload(MultipartFile file);
}

OssFileSericeImpl.java

package com.zxy.service.impl;

import cn.hutool.core.date.DateTime;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.zxy.service.OssFileService;
import com.zxy.utils.ConstantOssPropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
@Slf4j
public class OssFileServiceImpl implements OssFileService {

	@Override
	public String upload(MultipartFile file) {

		String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
		String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
		String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
		String bucketName = ConstantOssPropertiesUtils.BUCKET;

		String fileUrl = null;
		try {
			// 创建OSSClient实例
			OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
			// 获取文件流
			InputStream inputStream = file.getInputStream();
			String filename = file.getOriginalFilename();
			//为防止文件名重复造成文件覆盖,生成随机值,添加到文件中
			String uuid = UUID.randomUUID().toString().replaceAll("-","");
			filename = uuid + filename;
			//按照当前日期创建文件夹,上传到当前文件夹里 eg:2022/08/11/38babc9a4b7d4af99bea6a1075f63212url1.png
			String timeUrl = new DateTime().toString("yyyy/MM/dd");
			filename = timeUrl + "/" + filename;
			// 调用方法,实现上传  参数2为上传文件(路径+)名称
			ossClient.putObject(bucketName, filename, inputStream);
			// 关闭OSSClient。
			ossClient.shutdown();
			// 返回上传后文件路径
			fileUrl = "https://" + bucketName + "." + endpoint + "/" + filename;
		} catch (IOException e) {
			e.printStackTrace();
			log.info("文件上传阿里云异常");
		}
		return fileUrl;
	}
}

ConstantOssPropertiesUtils.java

package com.zxy.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 读取配置文件的工具类
 */
@Component
public class ConstantOssPropertiesUtils implements InitializingBean {

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

	public static String EDNPOINT;
	public static String ACCESS_KEY_ID;
	public static String SECRECT;
	public static String BUCKET;

	@Override
	public void afterPropertiesSet() throws Exception {
		EDNPOINT = endpoint;
		ACCESS_KEY_ID = accessKeyId;
		SECRECT = secret;
		BUCKET = bucket;
	}
}

您可能感兴趣的与本文相关的镜像

GPT-oss:20b

GPT-oss:20b

图文对话
Gpt-oss

GPT OSS 是OpenAI 推出的重量级开放模型,面向强推理、智能体任务以及多样化开发场景

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值