import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
/**
* 与云存储服务 digitalocean spaces 通信工具类
* 使用 AWS S3 的SDK
*
* 20180131
* @author fengshi
* @version 1.0
*/
public class AWSS3Util {
private static final String CONTENT_TYPE_SVG = "image/svg+xml";
private static String downloadUrl;
private static String bucketName;
private static AWSS3Util awsS3Util;
private static AmazonS3 s3;
private static ObjectMetadata metadata;
private AWSS3Util(){
}
/**
* 上传公有文件
*
* @param filePath 待上传文件路径
* @param key 该文件在bucket中的文件名
* @param isPub 是否公有
* @return String 文件访问url
* @throws FileNotFoundException
*/
public String putObject(String filePath, String key, boolean isPub) throws FileNotFoundException{
return putObject(new File(filePath), key, isPub);
}
/**
*
* @param file 待上传文件
* @param key 该文件在bucket中的文件名
* @param isPub 上传文件是否公有
* @return String 文件访问url
* @throws FileNotFoundException
*/
public String putObject(File file, String key, boolean isPub) throws FileNotFoundException{
return putObject(new FileInputStream(file), key, isPub);
}
/**
* @param inputStream 待上传文件流
* @param key 该文件在bucket中的文件名
* @param isPub 上传文件是否公有
* @return String 文件访问url
* @throws FileNotFoundException
*/
public String putObject(InputStream inputStream, String key, boolean isPub) throws FileNotFoundException{
PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream, metadata);
if(isPub) request.setCannedAcl(CannedAccessControlList.PublicRead);
s3.putObject(request);
return isPub ? new StringBuffer().append(downloadUrl).append(key).toString() : getPublicURL(key, null);
}
/**
* 获取文件公有链接
*
* @param key 该文件在bucket中的文件名
* @param expirationDate 设置过期时间 无过期时间传null
* @return
*/
public String getPublicURL(String key, Date expirationDate){
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, key);
if (null != expirationDate) urlRequest.setExpiration(expirationDate);
return s3.generatePresignedUrl(urlRequest).toString();
}
public static AWSS3Util getInstance(){
if(awsS3Util == null){
synchronized (AWSS3Util.class){
if (awsS3Util == null) {
awsS3Util = new AWSS3Util();
Properties pro = PropertiesUtils.getProperties();
s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(pro.getProperty("aws.accessKey"), pro.getProperty("aws.secretKey"))))
.withEndpointConfiguration(new EndpointConfiguration(pro.getProperty("aws.endpointUrl"), pro.getProperty("aws.region")))
.build();
metadata = new ObjectMetadata();
metadata.setContentType(CONTENT_TYPE_SVG);//目前只用于上传svg v1.0
downloadUrl = pro.getProperty("aws.downloadUrl");
bucketName = pro.getProperty("aws.bucketName");
}
}
}
return awsS3Util;
}
}
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.257</version>
</dependency>
关于创建连接时几个参数的对应关系:
这是我在DigitalOcean Spaces网页后台上传的文件生成的地址
https://hachi.ams3.digitaloceanspaces.com/batik_test10.svg
hachi是bucketName
region是ams3
endpointUrl是ams3.digitaloceanspaces.com
accessKey和secretKey去DigitalOcean Spaces创建
比较坑的是secretKey只在第一次创建accessKey时能看到 我创建第一个accessKey的时候没注意找了好久secretKey
本文介绍了一款针对DigitalOceanSpaces云存储服务的JavaSDK工具类实现,详细展示了如何使用该工具类上传文件并设置访问权限,同时提供了获取文件公共链接的方法。通过此工具类,开发者可以轻松地与DigitalOceanSpaces进行交互,实现文件的上传和管理。
57

被折叠的 条评论
为什么被折叠?



