最近有接入OSS的需求,下面讲讲如何接入:
首先有个OSS服务器,再创建Bucket,可以下载一个OSS客户端,页面如下
下面步入正题
jar包:
配置文件:com.aliyun.oss aliyun-sdk-oss 2.2.1
oss: ossaddress: oss-cn-hangzhou.aliyuncs.com(你的OSS服务器地址) accessKeyId: 你的ID accessKeySecret: 你的密码 bucketName: 你的bucket名称 --》跟服务器地址拼在一起就是你的访问地址 filePath: images/ 你的存储文件地址
读取配置文件,我这边是用的springboot ,配置文件是yml
@Component @ConfigurationProperties(prefix = "oss") public class OssProperties { private String ossaddress; public String getOssaddress() { return ossaddress; } public void setOssaddress(String ossaddress) { this.ossaddress = ossaddress; } private String accessKeyId; private String accessKeySecret; private String bucketName; private String filePath; public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public String getAccessKeyId() { return accessKeyId; } public String getAccessKeySecret() { return accessKeySecret; }
然后编写工具类
@Component public class OSSUtil { //log private final Logger LOG = LoggerFactory.getLogger(OSSUtil.class); @Autowired OssProperties ossProperties; /** * 获取阿里云OSS客户端对象 * */ public OSSClient getOSSClient(){ return new OSSClient(ossProperties.getOssAddress,ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret()); } /** * 新建Bucket --Bucket权限:私有 * @param bucketName bucket名称 * @return true 新建Bucket成功 * */ public boolean createBucket(OSSClient client, String bucketName){ Bucket bucket = client.createBucket(bucketName); return bucketName.equals(bucket.getName()); } /** * 删除Bucket * @param bucketName bucket名称 * */ public void deleteBucket(OSSClient client, String bucketName){ client.deleteBucket(bucketName); } /** * 向阿里云的OSS存储中存储文件 --file也可以用InputStream替代 * @param client OSS客户端 * @param file 上传文件 * @return String 唯一MD5数字签名 * */ public String uploadObject2OSS(OSSClient client, File file) { String resultStr = null; try { InputStream is = new FileInputStream(file); String fileName = file.getName(); Long fileSize = file.length(); //创建上传Object的Metadata ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(is.available()); metadata.setCacheControl("no-cache"); metadata.setHeader("Pragma", "no-cache"); metadata.setContentEncoding("utf-8"); metadata.setContentType(getContentType(fileName)); metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte."); //上传文件 PutObjectResult putResult = client.putObject(ossProperties.getBucketName(), ossProperties.getFilePath() + fileName, is, metadata); //解析结果 resultStr = putResult.getETag(); is.close(); } catch (Exception e) { LOG.error("上传阿里云OSS服务器异常." + e.getMessage(), e); } return resultStr; } /** * 根据key获取OSS服务器上的文件输入流 * @param client OSS客户端 * @param bucketName bucket名称 * @param diskName 文件路径 * @param key Bucket下的文件的路径名+文件名 */ public InputStream getOSS2InputStream(OSSClient client, String bucketName, String filePath, String key){ OSSObject ossObj = client.getObject(bucketName, filePath + key); return ossObj.getObjectContent(); } /** * 根据key删除OSS服务器上的文件 * @param client OSS客户端 * @param bucketName bucket名称 * @param diskName 文件路径 * @param key Bucket下的文件的路径名+文件名 */ public void deleteFile(OSSClient client, String bucketName, String filePath, String key){ client.deleteObject(bucketName, filePath+ key); LOG.info("删除" + bucketName + "下的文件" + diskName + key + "成功"); } /** * 通过文件名判断并获取OSS服务文件上传时文件的contentType * @param fileName 文件名 * @return 文件的contentType */ public final String getContentType(String fileName){ String fileExtension = fileName.substring(fileName.lastIndexOf(".")+1); if("bmp".equalsIgnoreCase(fileExtension)) return "image/bmp"; if("gif".equalsIgnoreCase(fileExtension)) return "image/gif"; if("jpeg".equalsIgnoreCase(fileExtension) || "jpg".equalsIgnoreCase(fileExtension) ) return "image/jpeg"; if("png".equalsIgnoreCase(fileExtension)) return "image/png"; if("html".equalsIgnoreCase(fileExtension)) return "text/html"; if("txt".equalsIgnoreCase(fileExtension)) return "text/plain"; if("vsd".equalsIgnoreCase(fileExtension)) return "application/vnd.visio"; if("ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) return "application/vnd.ms-powerpoint"; if("doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) return "application/msword"; if("xml".equalsIgnoreCase(fileExtension)) return "text/xml"; return "text/html"; } /** * 销毁 */ public void destory(OSSClient client) { client.shutdown(); } }
最后上传测试:
@Autowired OSSUtil ossUtil;
@Test public void testUploadImage(){ OSSClient client = ossUtil.getOSSClient(); String flilePathName = "E:\\1.png"; File file = new File(flilePathName); String md5key = ossUtil.uploadObject2OSS(client, file); System.out.print("上传后的文件MD5数字唯一签名:" + md5key);
ossUtil.destory(client);
}完成,后续根据自己的业务调用更多的接口。
我是要实现图片上传,再加图片读取功能,在读取图片时不需要验证,所以把bucket开发读取权限;
后面有个OSS跨域访问的坑,现在已经找到解决方案:
在上传图片时加入以下头声明
metadata.setHeader("Access-Control-Allow-Origin","*"); metadata.setHeader("Access-Control-Allow-Methods","POST GET HEAD PUT DELETE"); metadata.setHeader("Access-Control-Max-Age","3600");