Aliyun SDK文件上传

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.autotest.qa</groupId>

  <artifactId>uploadmp4</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>uploadmp4</name>

  <url>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8

</project.build.sourceEncoding>

  </properties>

  <dependencies>

 

   <!-- 增加aliyun SDK依赖 --> 

   <dependency>

       <groupId>com.aliyun.oss</groupId>

       <artifactId>aliyun-sdk-oss</artifactId>

       <version>2.7.0</version>

   </dependency>

    

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

   

  </dependencies>

</project>

 

 

文件断点续传,传完之后通过ossClient.generatePresignedUrl(bucketName, key, expiration)获取返回的URL,若访问该URL可以正常打开上传的视频,则说明上传成功

 

import java.io.IOException;

import java.net.URL;

import java.util.Date;

import com.aliyun.oss.ClientException;

import com.aliyun.oss.OSSClient;

import com.aliyun.oss.OSSException;

import com.aliyun.oss.model.CompleteMultipartUploadResult;

import com.aliyun.oss.model.UploadFileRequest;

import com.aliyun.oss.model.UploadFileResult;

 

public class UploadFiles {

  

   private  String endpoint;

   private  String bucketName;

  

    private String accessKeyId;

    private String accessKeySecret;

    private String securityToken;

      

    private String key;

    private static String uploadFile;

   

    private static OSSClient ossClient;

   

    public UploadFiles (String endpoint, String accessKeyId, String accessKeySecret, String securityToken) {

       this.endpoint = endpoint;

       this.accessKeyId = accessKeyId;

       this.accessKeySecret = accessKeySecret;

       this.securityToken = securityToken;

    }

   

    //断点续传上传

    public void mulUpload(String bucketName, String key,String uploadFile) throws IOException {

      

       ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);

       

        try {

            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);

            // 待上传的本地文件

            uploadFileRequest.setUploadFile(uploadFile);

            // 设置并发下载数,默认1

            uploadFileRequest.setTaskNum(5);

            // 设置分片大小,默认100KB

            uploadFileRequest.setPartSize(1024 * 1024 * 1);

            // 开启断点续传,默认关闭

            uploadFileRequest.setEnableCheckpoint(true);

                    

            UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);

                    

            CompleteMultipartUploadResult multipartUploadResult =

                    uploadResult.getMultipartUploadResult();

           

            //System.out.println("RequestId:  " + multipartUploadResult.getRequestId());

                            

        } 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.getErrorCode());

            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 (Throwable e) {

            e.printStackTrace();

        } finally {

            ossClient.shutdown();

        }

      

    }

   

    //获取文件的URL

    public String getUrl(String bucketName, String key) {

        // 设置URL过期时间为10  3600l* 1000*24*365*10 改为3600

        Date expiration = new Date(new Date().getTime() + 3600*1000);

        // 生成URL

        URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);

        if (url != null) {

          return url.toString();

        }

        return null;

      }

   public static void main(String[] args) throws IOException {

     

      String endpoint = "oss-cn-shenzhen.aliyuncs.com";

      String bucketName = "XXXX";   

      String accessKeyId = "XXXX";

       String accessKeySecret = "XXXX";

       String securityToken ="XXXX";

       //String uploadFile = "E:\\Jerry_test\\Jmeter\\AutoScript\\ videofile.MP4";

       String uploadFile = "src/main/resources/videofile.MP4";

       long timeNow = new Date().getTime();

       String key = "fv_" + timeNow + ".mp4";

       UploadFiles uploadFiles = new UploadFiles(endpoint, accessKeyId, accessKeySecret, securityToken);

       uploadFiles.mulUpload(bucketName, key,uploadFile);

       String fileUrl = uploadFiles.getUrl(bucketName, key);

       System.out.println(fileUrl);

   }

}

 

 

阿里云SDK帮助文档:https://help.aliyun.com/document_detail/32010.html?spm=5176.doc32069.6.662.9mmCDS

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值