项目使用阿里云OSS存储

步骤1,2,3,4都是开通阿里云oss

  1. 阿里云开通对象存储OSS服务
    开通OSS

  2. 搞一个access-key
    accessKey

  3. 搞一个子用户
    子用户

  4. 创建个用户, 并且给权限! 创建AccessKey同时记录下来, 关闭就没了
    创建用户

  5. 项目中引入依赖

    <dependency>
    	<groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
    </dependency>
    
  6. 在配置中配置

    spring:
      cloud:
    	alicloud:
          access-key: <用户的access-key>
          secret-key: <用户的secret-key>
          oss:
            endpoint: oss-cn-beijing.aliyuncs.com
    
  7. 方法中就可以用了

    @Resource
    OSSClient sooClient;
    
    public void test() {
    	String objectName = "文件名";
    	String filePath= "文件路径";
    	InputStream inputStream = new FileInputStream(filePath);
        // 创建PutObject请求。
        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObject请求。
            ossClient.putObject("edu-haozhancc", objectName, inputStream);
        } 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());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    

就ok了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值