欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > Springboot定义阿里云oss工具类

Springboot定义阿里云oss工具类

2025/2/23 16:42:33 来源:https://blog.csdn.net/weixin_53961667/article/details/140478969  浏览:    关键词:Springboot定义阿里云oss工具类

Springboot定义阿里云oss工具类


文章目录

      • Springboot定义阿里云oss工具类
        • 1、定义OSS相关配置
        • 2、读取OSS配置
        • 3、生成OSS工具类对象
        • 4、定义使用工具类

1、定义OSS相关配置

首先,在 application.yml 文件中定义阿里云 OSS 的相关配置信息。这些配置包括 endpointaccess-key-idaccess-key-secretbucket-name

alioss:endpoint: oss-cn-hangzhou.aliyuncs.comaccess-key-id: you_key_idaccess-key-secret: you_key_secretbucket-name: your_bucket_name
2、读取OSS配置

接下来,创建一个 AliOssProperties 类,用于读取 application.yml 中的配置。

@Component
@ConfigurationProperties(prefix = "oss.alioss")
@Data
public class AliOssProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;}
3、生成OSS工具类对象

为了生成 OSS 工具类对象,我们需要创建一个配置类 OssConfiguration,并在其中定义一个 AliOssUtil 的 Bean。

/*** 配置类,用于创建AliOssUtil对象*/
@Configuration
@Slf4j
public class OssConfiguration {@Bean@ConditionalOnMissingBeanpublic AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}
4、定义使用工具类

最后,定义 AliOssUtil 工具类,实现文件上传功能。

@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;/*** 文件上传** @param bytes* @param objectName* @return*/public String upload(byte[] bytes, String objectName) {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建PutObject请求。ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}//文件访问路径规则 https://BucketName.Endpoint/ObjectNameStringBuilder stringBuilder = new StringBuilder("https://");stringBuilder.append(bucketName).append(".").append(endpoint).append("/").append(objectName);log.info("文件上传到:{}", stringBuilder.toString());return stringBuilder.toString();}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词