该案例是OSS Java SDK的示例程序,您可以修改endpoint、accessKeyId、accessKeySecret、bucketName后直接运行。
本示例中的并不包括OSS Java SDK的所有功能,详细功能及使用方法,请参看“SDK手册 > Java-SDK”,
链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/preface.html?spm=5176.docoss/sdk/java-sdk/
调用OSS Java SDK的方法时,抛出异常表示有错误发生;没有抛出异常表示成功执行。
当错误发生时,OSS Java SDK的方法会抛出异常,异常中包括错误码、错误信息,详细请参看“SDK手册 > Java-SDK > 异常处理”,
链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/exception.html?spm=5176.docoss/api-reference/error-response
OSS控制台可以直观的看到您调用OSS Java SDK的结果,OSS控制台地址是:https://oss.console.aliyun.com/index#/
OSS控制台使用方法请参看文档中心的“控制台用户指南”指南的链接地址是:
https://help.aliyun.com/document_detail/oss/getting-started/get-started.html?spm=5176.docoss/user_guide
OSS的文档中心地址是:https://help.aliyun.com/document_detail/oss/user_guide/overview.html
OSS Java SDK的文档地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/install.html?spm=5176.docoss/sdk/java-sdk
OSS开发过程中常见问题链接地址:https://help.aliyun.com/document_detail/32024.html?spm=5176.product31815.6.665.JuvIOS
开发环境:
开发工具:eclipse4.4+jdk7 均64位
OSS Java SDK依赖下列包:
- aliyun-sdk-oss-2.2.1.jar
- hamcrest-core-1.1.jar
- jdom-1.1.jar
- commons-codec-1.9.jar
- httpclient-4.4.1.jar(重点)
- commons-logging-1.2.jar
- httpcore-4.4.1.jar(重点)
- log4j-1.2.15.jar
其中,log4j-1.2.15.jar是可选的,需要日志功能的时加入该包。其它包都是必不可少的。
解决办法:您的工程中在加入OSS Java SDK依赖的包,加入方法如下:
- 如果您的工程是Eclipse。请参考Java-SDK使用手册,”安装”-> “方式二:在Eclipse项目中导入工程依赖的包”;
- 如果您的工程是maven。则直接添加依赖即可如:
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-sdk-oss</artifactId>
- <version>2.5.0</version>
- </dependency>
代码:
阿里云OSS对象存储常量:
- package com.qianjiang.util.aliyunoss;
-
-
-
-
-
-
-
- public class OSSClientConstants {
-
- public static final String ENDPOINT = "oss-cn-shanghai.aliyuncs.com";
-
- public static final String ACCESS_KEY_ID = "LTAIRGxaf6yoUsj0";
-
- public static final String ACCESS_KEY_SECRET = "3gcfQkeWjaJ3tunuv4yyY4DStgpriz";
-
- public static final String BACKET_NAME = "uploadpicture";
-
- public static final String FOLDER="somnus/";
-
-
- }
阿里云基本参数详解:
endpoint是访问OSS的域名。如果您已经在OSS的控制台上 创建了Bucket,请在控制台上查看域名。
如果您还没有创建Bucket,endpoint选择请参看文档中心的“开发人员指南 > 基本概念 > 访问域名”,
链接地址是:https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/endpoint.html?spm=5176.docoss/user_guide/endpoint_region
endpoint的格式形如“http://oss-cn-hangzhou.aliyuncs.com/”,注意http://后不带bucket名称,
比如“http://bucket-name.oss-cn-hangzhou.aliyuncs.com”,是错误的endpoint,请去掉其中的“bucket-name”。
accessKeyId和accessKeySecret是OSS的访问密钥,您可以在控制台上创建和查看,
创建和查看访问密钥的链接地址是:https://ak-console.aliyun.com/#/
注意:accessKeyId和accessKeySecret前后都没有空格,从控制台复制时请检查并去除多余的空格。
Bucket用来管理所存储Object的存储空间,详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
Bucket命名规范如下:只能包括小写字母,数字和短横线(-),必须以小写字母或者数字开头,长度必须在3-63字节之间。
Object是OSS存储数据的基本单元,称为OSS的对象,也被称为OSS的文件。详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
Object命名规范如下:使用UTF-8编码,长度必须在1-1023字节之间,不能以“/”或者“\”字符开头。
就是上传文件名的名称,名称唯一
创建模拟文件夹
OSS是没有文件夹这个概念的,所有元素都是以Object来存储。
创建模拟文件夹本质上来说是创建了一个size为0的Object。
对于这个Object可以上传下载,只是控制台会对以”/“结尾的Object以文件夹的方式展示。
链接地址:https://help.aliyun.com/document_detail/32015.html?spm=5176.doc32013.2.5.ITwaY2
上传工具类:
- package com.qianjiang.util.aliyunoss;
-
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
-
- import org.apache.log4j.Logger;
-
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.model.Bucket;
- import com.aliyun.oss.model.OSSObject;
- import com.aliyun.oss.model.ObjectMetadata;
- import com.aliyun.oss.model.PutObjectResult;
-
-
-
-
-
-
-
- public class AliyunOSSClientUtil {
-
- private static Logger logger = Logger.getLogger(AliyunOSSClientUtil.class);
-
- private static String ENDPOINT;
-
- private static String ACCESS_KEY_ID;
-
- private static String ACCESS_KEY_SECRET;
-
- private static String BACKET_NAME;
-
- private static String FOLDER;
-
- static{
- ENDPOINT = OSSClientConstants.ENDPOINT;
- ACCESS_KEY_ID = OSSClientConstants.ACCESS_KEY_ID;
- ACCESS_KEY_SECRET = OSSClientConstants.ACCESS_KEY_SECRET;
- BACKET_NAME = OSSClientConstants.BACKET_NAME;
- FOLDER = OSSClientConstants.FOLDER;
- }
-
-
-
-
-
- public static OSSClient getOSSClient(){
- return new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);
- }
-
-
-
-
-
-
-
- public static String createBucketName(OSSClient ossClient,String bucketName){
-
- final String bucketNames=bucketName;
- if(!ossClient.doesBucketExist(bucketName)){
-
- Bucket bucket=ossClient.createBucket(bucketName);
- logger.info("创建存储空间成功");
- return bucket.getName();
- }
- return bucketNames;
- }
-
-
-
-
-
-
- public static void deleteBucket(OSSClient ossClient, String bucketName){
- ossClient.deleteBucket(bucketName);
- logger.info("删除" + bucketName + "Bucket成功");
- }
-
-
-
-
-
-
-
-
- public static String createFolder(OSSClient ossClient,String bucketName,String folder){
-
- final String keySuffixWithSlash =folder;
-
- if(!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)){
-
- ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
- logger.info("创建文件夹成功");
-
- OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
- String fileDir=object.getKey();
- return fileDir;
- }
- return keySuffixWithSlash;
- }
-
-
-
-
-
-
-
-
- public static void deleteFile(OSSClient ossClient, String bucketName, String folder, String key){
- ossClient.deleteObject(bucketName, folder + key);
- logger.info("删除" + bucketName + "下的文件" + folder + key + "成功");
- }
-
-
-
-
-
-
-
-
-
- public static String uploadObject2OSS(OSSClient ossClient, File file, String bucketName, String folder) {
- String resultStr = null;
- try {
-
- InputStream is = new FileInputStream(file);
-
- String fileName = file.getName();
-
- Long fileSize = file.length();
-
- 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 = ossClient.putObject(bucketName, folder + fileName, is, metadata);
-
- resultStr = putResult.getETag();
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
- }
- return resultStr;
- }
-
-
-
-
-
-
- public static String getContentType(String fileName){
-
- String fileExtension = fileName.substring(fileName.lastIndexOf("."));
- if(".bmp".equalsIgnoreCase(fileExtension)) {
- return "image/bmp";
- }
- if(".gif".equalsIgnoreCase(fileExtension)) {
- return "image/gif";
- }
- if(".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension) || ".png".equalsIgnoreCase(fileExtension) ) {
- return "image/jpeg";
- }
- 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 "image/jpeg";
- }
-
-
- public static void main(String[] args) {
-
- OSSClient ossClient=AliyunOSSClientUtil.getOSSClient();
-
- String files="D:\\image\\1010.jpg,D:\\image\\1111.jpg,D:\\image\\1212.jpg,D:\\image\\1313.jpg,D:\\image\\2222.jpg,D:\\image\\3333.jpg,"
- + "D:\\image\\4444.jpg,D:\\image\\5555.jpg,D:\\image\\6666.jpg,D:\\image\\7777.jpg,D:\\image\\8888.jpg";
- String[] file=files.split(",");
- for(String filename:file){
-
- File filess=new File(filename);
- String md5key = AliyunOSSClientUtil.uploadObject2OSS(ossClient, filess, BACKET_NAME, FOLDER);
- logger.info("上传后的文件MD5数字唯一签名:" + md5key);
-
- }
-
-
- }
-
-
- }
测试结果:
登录账户查看是否已经上传成功:
也可以下载OSS客户端查看:
客户端下载地址:http://download.csdn.net/detail/u014079773/9793762
登录客户端: