文件上传至阿里云之OssUtil工具类的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jqc874789596/article/details/84820337

什么是Oss?

阿里云对象存储服务(Object Storage Service,简称OSS)为您提供基于网络的数据存取服务。使用OSS,您可以通过网络随时存储和调用包括文本、图片、音频和视频等在内的各种非结构化数据文件。

简单来说,Oss支持任意类型的文件远程存储(文件类型包括但不仅限于图片,视频,文字),可以通过网络上传或下载分享,有着高性能,安全,稳定的特性。

更多Oss介绍参考此篇文章:点我

通过OssBroswer初识Oss

假设你已经学习过Oss相关知识,并且已拥有了Oss账号及存储空间。请先下载OssBroswer,以便后续的开发调试。

官网下载地址:点我获取下载地址

下载并安装完毕后,打开软件,输入对应的AK与AKS。

登陆成功可以看到已建的bucket(桶),后面的上传请上传至对应的bucket中即可。

Bucket下可以创建多个文件夹,可按照个人的分类习惯放入文件。

OssUtil的使用

网上工具类可以找到很多,大家百度就可以找到,这里我提供一个,该工具类有两个方法,第一个方法用来上传文件至阿里云,第二个方法用来获取指定路径下某个文件的访问路径(或下载路径),第三个方法用来删除Oss指定路径下的指定文件。

先贴代码。

/**
 * 
 * 上传数据之阿里云oss工具类
 */
@Component
public class OssUtil {
	
	public static final Logger LOGGER = LoggerFactory.getLogger(OssUtil.class);

	/**
	 * 生成访问图片地址有效期
	 */
	private static final int VISIT_URL_EXPIRATION = 365*10;

	/**
	 * uploadFile:上传文件到Oss
	 * @param accesKeyId accesKeyId
	 * @param accessKeySecret accessKeySecret
	 * @param endpoint endpoint
	 * @param savePath 存放路径
	 * @param bucketName bucket名字
	 * @param imageName 图片名字
	 * @param fileInputStream 图片流
	 * @param fileSize fileSize 图片大小
	 * @throws Exception
	 */
	public static void uploadFile(String accesKeyId,String accessKeySecret,String endpoint,String savePath,String bucketName,
			String imageName, InputStream fileInputStream,Long fileSize) throws Exception {
		OSSClient client = new OSSClient(endpoint, accesKeyId, accessKeySecret);
		try {
			ObjectMetadata objectMeta = new ObjectMetadata();
			objectMeta.setContentLength(fileSize);
			if (!savePath.endsWith("/")) {
				savePath = savePath + "/";
			}
			client.putObject(bucketName,savePath + imageName, fileInputStream, objectMeta);
		} catch (Exception e) {
			LOGGER.error("上传文件到oss出错", e);
			throw new Exception("上传文件到oss出错");
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
					client.shutdown();
				} catch (IOException e) {
					LOGGER.error("上传文件到oss出错", e);
				}
			}
		}
	}

   /**
    * 生成原图访问地址
    * @param ossAccessKeyId
    * @param ossAccessKeySecret
    * @param ossEndPoint
    * @param ossBucketName
    * @param imageName
    * @param path
    * @return
    * @throws Exception
    */
   public static String getVisitUrl( String ossAccessKeyId , String ossAccessKeySecret, String ossEndPoint, String ossBucketName, String path, String imageName ) throws Exception{
       OSSClient client = new OSSClient(ossEndPoint, ossAccessKeyId, ossAccessKeySecret);
       try {
           Calendar calendar = Calendar.getInstance();
           calendar.add(Calendar.DAY_OF_YEAR, VISIT_URL_EXPIRATION);
           return client.generatePresignedUrl(ossBucketName, path + "/" + imageName, calendar.getTime()).toString();
       } catch (Exception e) {
           throw new Exception("生成访问地址出错");
       }finally {
           client.shutdown();
       }
   }
	
	   /**
    * 删除Oss源文件
    * @param ossAccessKeyId
    * @param ossAccessKeySecret
    * @param ossEndPoint
    * @param ossBucketName
    * @param savePath
    * @param bucketName
	* @param imageName
    */
    public static void delete(String accesKeyId,String accessKeySecret,String endpoint,String savePath,String bucketName,
			String imageName) throws Exception {
    	OSSClient client = new OSSClient(endpoint, accesKeyId, accessKeySecret);
		try {
			if (!savePath.endsWith("/")) {
				savePath = savePath + "/";
			}
			client.deleteObject(bucketName, savePath+ imageName);
		} catch (Exception e) {
			LOGGER.error("删除oss文件出错", e);
			throw new Exception("删除oss文件出错");
		} finally {
					client.shutdown();
		}
    }

}

本文旨在简单介绍工具类的使用,不对上传整个流程作声明,后续文章会发上传插件的使用。

观察这三个方法所需参数,可以看出,有以下参数是必须的:

  1. accesKeyId
  2. accessKeySecret
  3. endpoint
  4. savePath
  5. bucketName
  6. imageName

注意一下,savePath写对应桶下文件夹的名字或路径即可(不包含桶名)

附:工具类下载地址

猜你喜欢

转载自blog.csdn.net/jqc874789596/article/details/84820337
今日推荐