oss上传图片java

服务端签名后直传
oss对象存储
前端传来数据,经由后端认证后返回数据告诉你是否允许上传,oss的账号密码保存在后端不容易暴露

使用java代码上传一个文件
1、在阿里云开通oss
开通后在控制台入口找到
在这里插入图片描述

2、引入oss依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

3、
配置类里面加入

    alicloud:
      access-key: LTAI5tQJdQMpFgUHRfRdH5bh
      secret-key: bCtc9GvH39WI4Axwr35unT5mDYDcuF
      oss:
        endpoint: oss-cn-nanjing.aliyuncs.com

3.1 endpoint这三个去阿里云oss配置对象你的Bucket 列表的概况里面去找
3.2 access-key、secret-key这俩在头像那里
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
接着按照步骤提示:别忘了配置用户权限为fulloss 这个加上就可以啦

4、编写测试上传方法:这里是通过服务端直接上传图片,这里注意图片名字不要有中文


    public void  testUpload() throws FileNotFoundException {
        //上传文件流
        InputStream inputStream = new FileInputStream("图片的路径");
        ossClient.putObject("bucketname","图片名字",inputStream);

        ossClient.shutdown();
        System.out.println("上传完成");

    }

这里要引入ossclient
在这里插入图片描述
最后可以在你的bucket中查看你上传的图片

下面是通过服务端签名前端直接上传到oss上
1、首先编辑OssController类 获得签证

package com.fx.mymall.thirdparty.controller;



import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class OssController {

    @Autowired
    OSS ossClient;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;
//    @Value("${spring.cloud.alicloud.oss.bucket}")
//    private String bucket;
    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;

    @RequestMapping("/oss/policy")
    public Map<String, String> police() {

        // 填写Bucket名称,例如examplebucket。
        String bucket = "mymall-freeda";
        // 填写Host地址,格式为https://bucketname.endpoint。
        String host = "https://" + bucket + "." + endpoint;
        // 设置上传回调URL,即回调服务器地址,用于处理应用服务器与OSS之间的通信。OSS会在文件上传完成后,把文件上传信息通过此回调URL发送给应用服务器。
//        String callbackUrl = "https://192.168.0.0:8888";
        // 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。
        String format = new SimpleDateFormat("yyyy-MM").format(new Date());
        //用户上传文件时指定的前缀。
        String dir = format + "/";
        Map<String, String> respMap = null;


        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap = new LinkedHashMap<String, String>();
            respMap.put("accessid", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            // respMap.put("expire", formatISO8601Date(expiration));

        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        }
        return respMap;
    }

}

2、获得签证之后开始写前端

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值