JAVA实现文件上传oss以及阿里云视频点播

引入依赖

  <!-- 阿里云oss依赖 -->
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.8.1</version>
</dependency>

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-vod</artifactId>
	  <version>2.15.2</version>
</dependency>

<dependency>
     <groupId>com.aliyun</groupId>
     <artifactId>aliyun-java-sdk-core</artifactId>
	 <version>4.3.3</version>
</dependency>

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-sdk-vod-upload</artifactId>
	  <version>1.4.11</version>
</dependency>

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
	 <version>1.2.28</version>
</dependency>

<dependency>
      <groupId>org.json</groupId>
     <artifactId>json</artifactId>
	 <version>20170516</version>
</dependency>

<dependency>
     <groupId>com.google.code.gson</groupId>
     <artifactId>gson</artifactId>
	 <version>2.8.2</version>
</dependency>

上传图片到oss


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.rw.oss.service.OssService;
import com.rw.oss.utils.ConstantPropertiesUtils;
import com.rw.oss.utils.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String uploadFileAvatar(MultipartFile file) {
        // Endpoint以杭州为例,其它Region请按实际情况填写。
        String endpoint = ConstantPropertiesUtils.END_POINT;
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "自己的ACCESS_KEY_ID";
        String accessKeySecret ="自己的ACCESS_KEY__SECRET";
        String bucketName ="自己的BUCKET_NAME";
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            String fileName = RandomUtils.genRandomNum();
			//这里的文件名我采用的是随机生成的 可以直接使用文件名字,但是使用文件名如果同名的文件会背新传上来的顶替掉
            ossClient.putObject(bucketName, fileName+".png", inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //返回上传之后路径
            String url = "https://"+bucketName+"."+endpoint+"/"+fileName+".png";
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

视频点播

1、初始化服务
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
2、获取视频ID
 public static void getPlayUrl()throws ClientException {
        //获取视频id
        DefaultAcsClient client = InitObject.initVodClient("自己的ACCESS_KEY_ID", "自己的ACCESS_KEY__SECRET");
        GetPlayInfoRequest request=new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        request.setVideoId("视频id");
        response=client.getAcsResponse(request);

        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
        }
        //Base信息
        System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
    }

2、获取私密播放视频凭证
 //获取视频播放凭证
    public static void getPlayAuth() throws Exception {
        DefaultAcsClient client = InitObject.initVodClient("自己的ACCESS_KEY_ID", "自己的ACCESS_KEY__SECRET");
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        response = getVideoPlayAuth(client);
        //播放凭证
        System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
        //VideoMeta信息
        System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");

        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }
    public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId("视频id");
        return client.getAcsResponse(request);
    }
详细请参考阿里云使用手册

oss详细使用手册

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值