OSS上传、下载、判断文件是否存在

package com.creditcard.merch.service.common;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.UploadFileRequest;
import com.creditcard.merch.common.CommonSystemConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Component
public class OssService {

private static final Logger log = LoggerFactory.getLogger(OssService.class);

private  String ossWriteUrl =CommonSystemConfig.configMap.get("oss_write_url");

private  String ossReadUrl = CommonSystemConfig.configMap.get("oss_read_url");

private  String accessId = CommonSystemConfig.configMap.get("access_id");

private  String accessKey =CommonSystemConfig.configMap.get("access_key"); 

private  String bucKey =CommonSystemConfig.configMap.get("buc_key");  

/**
 * 简单上传
* @Title: commonUpload
* @param localFile
* @param remoteFileName    设定文件
 */
public void upload(String localFile, String remoteFileName) {
	// endpoint以杭州为例,其它region请按实际情况填写
	String endpoint = ossWriteUrl;
	// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
	String accessKeyId = accessId;
	String accessKeySecret = accessKey;
	// 创建OSSClient实例
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	try {
		File file = new File(localFile);
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentLength(file.length());
		objectMeta.setContentType(ContentTypeService.getContentType(localFile)); // 设置上传内容类型

		InputStream input = new FileInputStream(file);

		log.info("登录oss服务器成功");

		ossClient.putObject(bucKey,remoteFileName ,input, objectMeta);
	} catch (Exception e) {
		log.error("上传异常:{}", e);
	} finally {
		// 关闭client
		ossClient.shutdown();
		log.info("关闭client");
	}
}

/**
 * 判断文件是否存在
 * @param remoteFileName
 * @return
 * 2018年11月23日
 */
public Boolean doesObjectExist(String remoteFileName) {
	// endpoint以杭州为例,其它region请按实际情况填写
	String endpoint = ossWriteUrl;
	// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
	String accessKeyId = accessId;
	String accessKeySecret = accessKey;
	// 创建OSSClient实例
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	Boolean result=ossClient.doesObjectExist( bucKey,remoteFileName);
	// 关闭OSSClient。
	ossClient.shutdown();
	return result;
}

/**
 * 下载文件
 * @param remoteFileName
 * @return
 * 2018年11月23日
 */
public Boolean getObject(String remoteFileName,String uploadpath) {
	Boolean result=false;
	// endpoint以杭州为例,其它region请按实际情况填写
	String endpoint = ossWriteUrl;
	// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
	String accessKeyId = accessId;
	String accessKeySecret = accessKey;
	// 创建OSSClient实例
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	try {
		ossClient.getObject(new GetObjectRequest(bucKey,remoteFileName),new File(uploadpath));
		result=true;
	}  catch (Exception e) {
		log.error("下载异常:{}", e);
	} finally {
		// 关闭client
		ossClient.shutdown();
		log.info("关闭client");
	}
	return result;
}

}

你可能感兴趣的:(OSS上传、下载、判断文件是否存在)