pom.xml 经过实测,不同版本代码可能有区别,以下是针对我的工具类的版本


<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.10.26</version>
</dependency>


操作类


package com.ccjt.eip.web.common;

import com.amazonaws.AmazonClientException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.net.URL;
import java.time.LocalTime;

/*
* 云盘操作类
* @param
* @return
*/
public class AwsUtil {

    final static String AWS_ACCESS_KEY = "";
    final static String AWS_SECRET_KEY = "";
    public static AmazonS3 s3Client;
    static {
        AWSCredentials awsCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY,AWS_SECRET_KEY);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);

        AwsClientBuilder.EndpointConfiguration endpointConfig =
                new AwsClientBuilder.EndpointConfiguration("http://oss.verycloud.cn", "cn-north-1");//服务端点、签名区域

        ClientConfiguration config = new ClientConfiguration();

//        s3Client = AmazonS3Client.builder()
//                .withEndpointConfiguration(endpointConfig)
//                .withClientConfiguration(config)
//                .withCredentials(awsCredentialsProvider)
//                .disableChunkedEncoding()
//                .withPathStyleAccessEnabled(true).build();

        s3Client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(endpointConfig)
                .withCredentials(awsCredentialsProvider)
                .build();
    }
    /*
     * 推送文件至云盘
    * @param [physicalPath 要推送到云盘的源文件全路径, bucketName 桶名, remoteFileName 桶内存放全路径含文件名后缀]
    * @return boolean
    */
    public static boolean putObject(String physicalPath,String bucketName,String remoteFileName){
        Preconditions.checkArgument(StringUtils.isNotBlank(physicalPath),"physicalPath为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(bucketName),"bucketName为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(remoteFileName),"remoteFileName为空");
        File file = new File(physicalPath);

        TransferManager tm = TransferManagerBuilder.standard()
                .withS3Client(s3Client)
                .build();
        // TransferManager processes all transfers asynchronously,
        // so this call will return immediately.
        Upload upload = tm.upload(
                bucketName, remoteFileName, file);
        System.out.println("上传开始:" + LocalTime.now());
        try {
            // Or you can block and wait for the upload to finish
            try {
                upload.waitForCompletion();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Upload complete.");
        } catch (AmazonClientException amazonClientException) {
            System.out.println("Unable to upload file, upload was aborted.");
            amazonClientException.printStackTrace();
        }
        System.out.println("上传结束:" + LocalTime.now());
        return true;
    }
    /*
     * 下载
    * @param [bucketName 桶名, remoteFileName 桶内存放全路径含文件名后缀]
    * @return java.lang.String
    */
    public static String download(String bucketName,String remoteFileName){
        Preconditions.checkArgument(StringUtils.isNotBlank(bucketName),"bucketName为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(remoteFileName),"remoteFileName为空");
        GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
        URL url = s3Client.generatePresignedUrl(urlRequest);
        return url.toString();
    }

}


实测


@Test
public void cloudDiskTest(){
    //分段上传
    String bucketName = "eip";
    String trueName = IdUtil.fastSimpleUUID() + ".zip";
    String remoteFileName = "dev" + "/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + "/" + trueName;
    if(AwsUtil.putObject("D:\\nginx-1.15.7\\html\\eip\\upload\\doc\\android-sdk_r24.4.1-windows.zip",bucketName,remoteFileName)){//上传成功就返回即时可下载的地址
        System.out.println(AwsUtil.downloadUrl(bucketName,remoteFileName));
    }
}