Springboot 阿里云对象存储OSS

云服务

通过互联网对外提供的各类型的服务,比如:语音、短信、邮件、视频直播、对象存储

当在项目开发中需要某些服务时,就无需自己开发了,可以直接使用别人提供好的、现成的云服务即可;这样就降低了开发难度、提升了效率

阿里云对象存储OSS

使用对象存储服务之后,当项目中再次涉及上传等业务,就无需在服务器本地磁盘中存储文件了,直接将收到的文件上传到OSS,

由OSS帮助进行存储和管理,同时OSS存储服务还口语保证存储内容的安全性

过程:前端上传文件到服务端,服务端及那个文件上传到OSS进行存储

快速使用示例

java 复制代码
package com.wzb.OSS20241008;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.wzb.pojo20241008.Result;
import org.springframework.web.bind.annotation.PostMapping;

@RestController
public class OSSController {
    // 使用阿里云对象存储OSS的步骤:
    // 1.创建bucket
    // 2.获取AccessKey(密钥)
    // 3.引入相关依赖
    // 4.案例集成OSS

    // Bucket:存储空间是用户用于存储对象(Object,也就是文件)的容器,所有的对象都必须隶属于某个存储空间
    @PostMapping("/OSSupload")
    public Result OSSUpload(String userName, Integer age, MultipartFile image) throws com.aliyuncs.exceptions.ClientException {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-chengdu.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "wzb-study";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                credentialsProvider.getCredentials().getAccessKeyId(),
                credentialsProvider.getCredentials().getSecretAccessKey()
        );

        try {
            // 创建存储空间。
            ossClient.createBucket(bucketName);

        } 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();
            }
        }
        return Result.success();
    }
}
相关推荐
飞翔的佩奇8 分钟前
Java项目:基于SSM框架实现的忘忧小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告】
java·数据库·mysql·vue·毕业设计·ssm框架·小区物业管理系统
阿蒙Amon8 分钟前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
程序员爱钓鱼15 分钟前
Go语言泛型-泛型约束与实践
前端·后端·go
寻月隐君16 分钟前
保姆级教程:Zsh + Oh My Zsh 终极配置,让你的 Ubuntu 终端效率倍增
linux·后端·命令行
程序员爱钓鱼18 分钟前
Go语言泛型-泛型对代码结构的优化
后端·google·go
这里有鱼汤23 分钟前
“对象”?对象你个头!——Python世界观彻底崩塌的一天
后端·python
RainbowSea25 分钟前
跨域问题(Allow CORS)解决(3 种方法)
java·spring boot·后端
掘金-我是哪吒27 分钟前
分布式微服务系统架构第155集:JavaPlus技术文档平台日更-Java线程池实现原理
java·分布式·微服务·云原生·架构
RainbowSea30 分钟前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis
尘浮72832 分钟前
60天python训练计划----day59
开发语言·python