<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.736</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.736</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
import com.alibaba.fastjson.JSON;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.URL;
import java.util.List;
/**
* ceph 工具类
* @author nick
*/
@Slf4j
public class CephUtils{
public void CephUtils(){}
/**
* 【你的 access_key】
*/
private static final String AWS_ACCESS_KEY = "XH084XXXXA3Y0EZT2CX";
/**
* 【你的 aws_secret_key】
*/
private static final String AWS_SECRET_KEY = "rJ4Xs9wACXXXXXognDwEP31KmUzv1vV9M24BWT88";
/**
* 【你的 endpoint】
*/
private static final String ENDPOINT = "http://127.0.0.1:7480";
private static AmazonS3 conn;
/**
* 静态块:初始化S3的连接对象AmazonS3! 需要3个参数:AWS_ACCESS_KEY,AWS_SECRET_KEY
*/
static {
AWSCredentials awsCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
conn = new AmazonS3Client(awsCredentials, clientConfig);
conn.setEndpoint(ENDPOINT);
}
/**
* 获取ceph的所有列表
* @return
*/
private static List<Bucket> getBucketList(){
List<Bucket> buckets = conn.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.getName() + "\t" +
StringUtils.fromDate(bucket.getCreationDate()));
}
return buckets;
}
/**
* 创建对象的bucket
* @param bucketName
* @return
*/
private static ObjectListing getObjectListing(String bucketName){
Bucket bucket = conn.createBucket(bucketName);
ObjectListing objects = conn.listObjects(bucket.getName());
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
System.out.println(objectSummary.getKey() + "\t" +
objectSummary.getSize() + "\t" +
StringUtils.fromDate(objectSummary.getLastModified()));
}
objects = conn.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
return objects;
}
/**
* 创建对象bucket
* @param bucketName
*/
private static void createBucket(String bucketName){
Bucket bucket = conn.createBucket(bucketName);
System.out.println(JSON.toJSONString(bucket));
}
/**
* 删除对象bucket
* @param bucketName
*/
private static void deleteBucket(String bucketName){
conn.deleteBucket(bucketName);
}
/**
* 上传字符串生成文件
* @param bucketName
* @param fileName
* @param text
*/
private static void uploadStream(String bucketName, String fileName, String text){
ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes());
PutObjectResult putObjectResult = conn.putObject(bucketName, fileName, input, new ObjectMetadata());
System.out.println(JSON.toJSONString(putObjectResult));
}
/**
* 修改文件权限 public
* @param bucketName
* @param fileName
*/
private static void modifyPub(String bucketName, String fileName){
conn.setObjectAcl(bucketName, fileName, CannedAccessControlList.PublicRead);
}
/**
* 修改文件权限 私有版
* @param bucketName
* @param fileName
*/
private static void modifyPri(String bucketName, String fileName){
conn.setObjectAcl(bucketName, fileName, CannedAccessControlList.Private);
}
/**
* 下载
* @param bucketName
* @param keyName
* @param dirName
*/
private static void downloadFile(String bucketName, String keyName, String dirName){
conn.getObject(
new GetObjectRequest(bucketName, keyName),
new File(dirName)
);
}
/**
* 删除文件
*/
private static void deleteObject(String bucketName, String fileName){
conn.deleteObject(bucketName, fileName);
}
/**
* 获取下载url 生成对象的下载 URLS (带签名和不带签名)
* @param bucketName
* @param keyName
* @return
*/
private static URL geturl(String bucketName, String keyName){
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, keyName);
return conn.generatePresignedUrl(request);
}
/**
* 上传本地文件到AWS S3
* @param file
* @param keyName
* @throws IOException
*/
public static void uploadToS3(String bucketName, File file, String keyName){
try {
PutObjectRequest request = new PutObjectRequest(bucketName, keyName, file);
PutObjectResult putObjectResult = conn.putObject(request);
} catch (Exception e) {
log.info("上传文件异常:{}",e);
}
}
/**
* 上传文件返回url
* @param bucketName
* @param file
* @param keyName
* @return
*/
public static URL uploadFileToUrl(String bucketName, File file, String keyName){
try {
PutObjectRequest request = new PutObjectRequest(bucketName, keyName, file);
conn.putObject(request);
} catch (Exception e) {
log.info("上传文件异常:{}",e);
}
GeneratePresignedUrlRequest requests = new GeneratePresignedUrlRequest(bucketName, keyName);
return conn.generatePresignedUrl(requests);
}
/**
* 上传InputStream文件
* @param bucketName
* @param fileName
* @param input
*/
private static void uploadInputStream(String bucketName, String fileName, InputStream input){
PutObjectResult putObjectResult = conn.putObject(bucketName, fileName, input, new ObjectMetadata());
System.out.println(JSON.toJSONString(putObjectResult));
}
/**
* 删除文件
* @param bucketName
* @param fileName
* @param contents
*/
private static void uploadByte(String bucketName, String fileName, byte[] contents){
try (ByteArrayInputStream input = new ByteArrayInputStream(contents)) {
PutObjectResult putObjectResult = conn.putObject(bucketName, fileName, input, new ObjectMetadata());
System.out.println(JSON.toJSONString(putObjectResult));
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new IllegalStateException("文件上传到阿里云OSS服务报错!", e);
}
}
/**
* 下载相应的S3数据到本地文件系统
* @param s3Client
* @param bucketName
* @param key
* @param filePath
*/
public static void downloadFromS3(AmazonS3 s3Client, String bucketName, String key, String filePath){
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
if(object != null){
InputStream input = null;
FileOutputStream fileOutputStream = null;
byte[] data;
try {
//获取文件流
input = object.getObjectContent();
data = new byte[input.available()];
int len = 0;
fileOutputStream = new FileOutputStream(filePath);
while ((len = input.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (IOException e) {
log.error("下载文件异常:{}",e);
}finally{
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
log.error("关闭fileOutputStream异常:{}",e);
}
}
if(input!=null){
try {
input.close();
} catch (IOException e) {
log.error("关闭input异常:{}",e);
}
}
}
}
}
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[100];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.roncoo</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29</version>
</dependency>
import lombok.extern.slf4j.Slf4j;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.*;
import java.util.Arrays;
@Slf4j
public class FastDFSUtil {
private TrackerClient trackerClient;
private TrackerServer trackerServer;
private StorageServer storageServer;
private StorageClient storageClient;
public FastDFSUtil(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient(trackerServer, storageServer);
}
public String[] uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
return storageClient.upload_file(fileName, extName, metas);
}
public String[] uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String[] uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
public String[] uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
return storageClient.upload_file(fileContent, extName, metas);
}
public String[] uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String[] uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
private static final String CONF_NAME = "fdfstest.conf";
public void initStorageClient() throws Exception {
ClientGlobal.init(CONF_NAME);
log.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
log.info("charset=" + ClientGlobal.g_charset);
com.roncoo.fastdfs.TrackerClient trackers = new com.roncoo.fastdfs.TrackerClient();
com.roncoo.fastdfs.TrackerServer trackerServers = trackers.getTrackerServer();
com.roncoo.fastdfs.StorageServer storageServer = null;
com.roncoo.fastdfs.StorageClient storageClients = new com.roncoo.fastdfs.StorageClient(trackerServers, storageServer);
}
public void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = new File(fileName);
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(fbyte);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
}
}
public void upload() throws Exception{
NameValuePair[] metaList = new NameValuePair[1];
String local_filename = "build.PNG";
metaList[0] = new NameValuePair("fileName", local_filename);
File file = new File("C:/Users/chengdu/Desktop/build.PNG");
InputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte[] bytes = new byte[length];
inputStream.read(bytes);
String[] result = storageClient.upload_file(bytes, null, metaList);
log.info("result {}", Arrays.asList(result));
}
public void download() throws Exception {
String[] uploadresult = {"group1", "M00/00/00/wKgBZV0phl2ASV1nAACk1tFxwrM3814331"};
byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
String local_filename = "build.PNG";
writeByteToFile(result, local_filename);
File file = new File(local_filename);
}
public void testUploadDownload() throws Exception {
NameValuePair[] metaList = new NameValuePair[1];
String local_filename = "build.PNG";
metaList[0] = new NameValuePair("fileName", local_filename);
File file = new File("C:/Users/chengdu/Desktop/build.PNG");
InputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte[] bytes = new byte[length];
inputStream.read(bytes);
String[] result = storageClient.upload_file(bytes, null, metaList);
byte[] resultbytes = storageClient.download_file(result[0], result[1]);
writeByteToFile(resultbytes, local_filename);
File downfile = new File(local_filename);
}
}
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 10.0.11.245:22122
tracker_server = 10.0.11.246:22122
## fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 185.245.40.70:22122
## Whether to open the connection pool, if not, create a new connection every time
fastdfs.connection_pool.enabled = true
## max_count_per_entry: max connection count per host:port , 0 is not limit
fastdfs.connection_pool.max_count_per_entry = 500
## connections whose the idle time exceeds this time will be closed, unit: second, default value is 3600
fastdfs.connection_pool.max_idle_time = 3600
## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000
参考:https://gitee.com/roncoocom/fastdfs-client-java
参考:https://github.com/happyfish100/fastdfs-client-java