阿里云OSS云存储简单使用
2018-04-26 本文已影响193人
帅可儿妞
最近做项目的时候需要存储静态图片,但是有一个问题,我们的前台系统和后台系统采用Maven继承的方式实现,即前台系统web和后台系统mgr都依赖一个名为base的jar包,那么前后台系统的图片上传共享就出现了问题,开始我们在阿里云上搭建了一个FTP服务器,但不幸的是,我们的小程序不支持FTP的访问(表示很纳闷),所以就选择了阿里云的OSS存储,由于工期较紧所以就把上传的任务放在了服务器上,其实更好的方案是在客户端中上传,服务器只负责保存文件名。我在此小记,希望可以帮助到您。
阿里云OSS云存简单使用步骤
-
开通OSS服务,并获取到如下信息:
- ENDPOINT:访问的域名
- ACCESSKEYID:键ID
- ACCESSKEYSECRET:键密钥
- BUCKETNAME:可选状态,只要权限够可以使用代码创建;
-
添加配置文件,把上面的信息配置在配置文件中,不多说
-
添加jar依赖,可以去OSS官网下载aliyun_java_sdk_yyyyMMdd.zip,其中也有各种源码文档资料示例等,如果你的项目使用Maven构建则可以使用如下Maven依赖:
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.3</version> </dependency>
-
编写一个工具类专门处理OSS文件上传,我的文件如下:
import java.io.File; import java.util.Iterator; import java.util.Map; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.CannedAccessControlList; import com.aliyun.oss.model.CreateBucketRequest; import com.aliyun.oss.model.PutObjectRequest; import lombok.extern.slf4j.Slf4j; @Slf4j public class OSSUtil { public static final String OSS_ENDPOINT = "OSS_ENDPOINT"; public static final String OSS_ACCESSKEYID = "OSS_ACCESSKEYID"; public static final String OSS_ACCESSKEYSECRET = "OSS_ACCESSKEYSECRET"; public static final String OSS_BUCKETNAME = "OSS_BUCKETNAME"; public static String endpoint = PropertiesUtil.getProperty(OSS_ENDPOINT); public static String accesskeyId = PropertiesUtil.getProperty(OSS_ACCESSKEYID); public static String accesskeySecret = PropertiesUtil.getProperty(OSS_ACCESSKEYSECRET); public static String bucketName = PropertiesUtil.getProperty(OSS_BUCKETNAME); public static boolean uploadFile2OSS(Map<String, File> filesMap) { OSSClient ossClient = new OSSClient(endpoint, accesskeyId, accesskeySecret); try { if (!ossClient.doesBucketExist(bucketName)) { System.out.println("Creating bucket " + bucketName + "\n"); ossClient.createBucket(bucketName); CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName); createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead); ossClient.createBucket(createBucketRequest); } log.info("#####################################开始上传...\n"); Iterator<String> iterator = filesMap.keySet().iterator(); while (iterator.hasNext()) { String filename = iterator.next(); ossClient.putObject(new PutObjectRequest(bucketName, filename, filesMap.get(filename))); } log.info("#####################################上传完成\n"); return true; } catch (OSSException oe) { log.error("Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason."); log.error("Error Message: " + oe.getErrorCode()); log.error("Error Code: " + oe.getErrorCode()); log.error("Request ID: " + oe.getRequestId()); log.error("Host ID: " + oe.getHostId()); return false; } catch (ClientException ce) { log.error("Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network."); log.error("Error Message: " + ce.getMessage()); return false; } finally { ossClient.shutdown(); } } }
-
在需要的地方调用工具类的方法即可如下:
// 上传文件到OSS Map<String, File> fileMap = Maps.newHashMap(); fileMap.put(filename, imgFile); fileMap.put(thumbnailName, thumbnailFile); boolean success = OSSUtil.uploadFile2OSS(fileMap); if (!success) { return ServerResponse.createErrorWithMessage("上传到OSS失败"); }