package com.ruoyi.hywz.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

public class OssUploadService {

	/**蒋新衡*/
//	private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
//	private static  String accessKeyId = "LTAI5tHVr9KJnCfyT47MQNSf";
//	private static String accessKeySecret = "IiJwSCw3MuXbxJnV0gEFsMBvxg7c6j";
//	private static String bucketName="jiangxinheng";

	/**衡阳线上*/
	private static String endpoint = "oss-hn-1-a.ops.sgmc.sgcc.com.cn";
	// accessKeyId 和 accessKeySecret 是先前创建用户生成的
	private static  String accessKeyId = "hCIXKxdzJ0mlkOKp";
	private static String accessKeySecret = "tPhkZ7FwqH5IFbYAId3z0Cp63WgvqC";
	private static String bucketName="hn-hywzqlcfxygk";

	public static BaseFile ossUpload(MultipartFile file) throws Exception {
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 创建OSSClient实例。
		String fileName = file.getOriginalFilename();
		String fileNameTemp = fileName.substring(0,fileName.lastIndexOf("."));
		String suffer = fileName.substring(fileName.lastIndexOf("."),fileName.length());
		fileName = fileNameTemp+String.valueOf(System.currentTimeMillis())+suffer;
		ossClient.putObject(bucketName, fileName, file.getInputStream());
		ossClient.shutdown();// 关闭OSSClient。
		BaseFile baseFile = new BaseFile();
		baseFile.setFileName(fileName);
		baseFile.setFilePath("http://" +bucketName + "." + endpoint + "/" + fileName);
		return baseFile;
	}
	public static BaseFile ossUpload(MultipartFile file, String ossFileName) throws Exception {
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 创建OSSClient实例。
		ossClient.putObject(bucketName, ossFileName, file.getInputStream());
		ossClient.shutdown();// 关闭OSSClient。
		BaseFile baseFile = new BaseFile();
		baseFile.setFileName(file.getOriginalFilename());
		baseFile.setFilePath("http://" +bucketName + "." + endpoint + "/" + ossFileName);
		return baseFile;
	}

}
