ceph s3 对象的上传和删除(JAVA demo)

ceph s3 文件的上传和删除

准备条件:
(1)搭建一个ceph集群

创建ceph集群
https://blog.csdn.net/yangkang1122/article/details/88681682
(2)创建一个ceph s3用户
创建ceph s3用户
https://blog.csdn.net/yangkang1122/article/details/88681826
创建s3用户之后,会得到SecretKey和AccessKey
(3)在ceph中创建一个桶bucket
一、创建springboot项目并添加所需要的依赖

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.ceph</groupId>
    <artifactId>libcephfs</artifactId>
    <version>0.80.5</version>
</dependency>
<dependency>
     <groupId>io.github.twonote</groupId>
     <artifactId>radosgw-admin4j</artifactId>
     <version>1.1.0</version>
</dependency>
<dependency>
     <groupId>com.amazonaws</groupId>
     <artifactId>aws-java-sdk-s3</artifactId>
     <version>1.11.534</version>
</dependency>

二、创建CEPHOperationServiceImpl类

@Service
public class CEPHOperationServiceImpl implements CEPHOperationService{
	
	@Autowired
	CEPHGetCephInfo cephInfo;
	Logger logger=Logger.getLogger(CEPHOperationServiceImpl.class.getName());
	
	public Map<String,Object>uploadFileObject(UploadFile uploadFile){
		Map<String,Object>result=new HashMap<String,Object>();
		Map<String,Object>resultMap=new HashMap<String,Object>();
		logger.info(cephInfo.toString());
		MultipartFile uploadfile=uploadFile.getFile();
		String upload_FileName=uploadfile.getOriginalFilename();
		//生成文件的key
		String fileKey=UUID.randomUUID().toString().replaceAll("-", "")+"."+getFileType(upload_FileName);
		
		AWSCredentials credentials = new BasicAWSCredentials(cephInfo.getAccessKey(), cephInfo.getSecretKey());
		ClientConfiguration clientConfig = new ClientConfiguration();
		clientConfig.setProtocol(Protocol.HTTP);
		AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);
		//设置连接站点
		String ceph_address="http://"+cephInfo.getCeph_ip()+":"+cephInfo.getCeph_port();
		conn.setEndpoint(ceph_address);
		try {
			 InputStream ins=uploadfile.getInputStream();
			 PutObjectRequest request = new PutObjectRequest(cephInfo.getBucketName(), fileKey,ins,new ObjectMetadata());
			 conn.putObject(request.withCannedAcl(CannedAccessControlList.PublicRead));
			 String url="http://192.168.100.163:7480"+"/"+cephInfo.getBucketName()+"/"+fileKey;
			 System.out.println(fileKey);
			 result.put("fileId", fileKey);
		     result.put("filePath", url);
		     resultMap.put("message","upload file success!");
		     resultMap.put("code",200);
		     resultMap.put("data", result);
		     return resultMap;
		}catch(Exception e) {
			e.printStackTrace();
			resultMap.put("message","upload file failed!");
			resultMap.put("code",250);
			result.put("data", "null");
			return resultMap;
		}	
	}
	
	public Map<String,Object>deleteFileObject(String object_key){
		
		Map<String,Object>resultMap=new HashMap<String,Object>();
		Map<String,Object>result=new HashMap<String,Object>();
		
		AWSCredentials credentials = new BasicAWSCredentials(cephInfo.getAccessKey(), cephInfo.getSecretKey());
		ClientConfiguration clientConfig = new ClientConfiguration();
		clientConfig.setProtocol(Protocol.HTTP);
		AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);
		//设置连接站点
		String ceph_address="http://"+cephInfo.getCeph_ip()+":"+cephInfo.getCeph_port();
		conn.setEndpoint(ceph_address);
		try {
			//删除对象,获取文件的key
//		    String fileKey=objectKey.getFileName();
		    //获取文件的URL
		    String url="http://192.168.100.163:7480"+"/"+cephInfo.getBucketName()+"/"+object_key;
		    conn.deleteObject(cephInfo.getBucketName(), object_key);
		    
		    result.put("fileId", object_key);
		    result.put("filePath", url);
		    resultMap.put("message","delete file success!");
		    resultMap.put("code", 200);
		    resultMap.put("data", result);
		    return resultMap;
		}catch(Exception e) {
			e.printStackTrace();
			resultMap.put("messaage", "delete file failed!");
			resultMap.put("code", 250);
			resultMap.put("data", "null");
			return resultMap;
		}
		
	}
	
	
	public String getFileType(String fileName){
		
		String[] strArray=fileName.split("\\.");
		int suffixIndex=strArray.length-1;
		return strArray[suffixIndex];
	}
}

你可能感兴趣的:(ceph s3 对象的上传和删除(JAVA demo))