记录开发内容demo-java华为云OBS上传

文章目录

  • 前言
  • 一、所需sdk
  • 二、tuil类
    • 1.demo
  • 总结


前言

华为云OBS上传


一、所需sdk

    
        com.huaweicloud
        esdk-obs-java
        3.21.12
    

二、tuil类

1.demo

代码如下(示例):

package com.pcwl.pc.controller;

import com.obs.services.ObsClient;
import com.pcwl.service.ElderService;
import com.pcwl.util.servlet.BaseController;
import com.pcwl.util.servlet.R;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;


//华为云文件控制器
@RestController
@RequestMapping("/hwyobs")

public class HuaWeiYunOBSController extends BaseController {



    //上传
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException
    {
        String endPoint = "http://obs.cn-north-4.myhuaweicloud.com";
        String ak = "x";
        String sk = "x";
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);

        String name = file.getOriginalFilename();
        name = name.substring(name.indexOf("."));
        String objectname = "x" + System.currentTimeMillis()+name;

        String url = "访问前缀";

        // localfile为待上传的本地文件路径,需要指定到具体的文件名
        obsClient.putObject("bucketName", objectname, file.getInputStream());

        String photoUrl = url + objectname;

        return data(photoUrl);
    }

}

总结

华为云OBS上传

你可能感兴趣的:(java,华为云,servlet)