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;
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值