百度云存储,即BCS(Baidu Cloud Storage),提供object网络存储服务,旨在利用百度在分布式以及网络方面的优势为开发人员提供安全、简单、高效的存储服务。
百度云存储提供了一系列简单易用的REST API接口、SDK、工具和方案,使得开发人员能够在不论什么时间、不论什么地点存储不论什么类型的数据。通过使用百度云存储服务,开发人员能够轻松地开发出扩展性强、稳定性好、安全高速的分布式网络服务;通过使用云存储服务提供的API、SDK及管理平台,开发人员也能够迅速开发出适合各种业务的网络程序。
百度云存储能够支持文本、多媒体、二进制等不论什么类型的数据,支持签名认证及ACL权限设置进行资源訪问控制,开发人员能够通过管理控制台直接进行页面上传、下载或通过REST API、Shell Tool、SDK、curl等方式实现上传、下载。
百度云存储服务主要提供下面功能供开发人员使用:
功能 | 描写叙述 | |
---|---|---|
1 | 支持不论什么类型数据的上传和下载 | 文本、多媒体、日志、二进制等类型数据 |
2 | 强大的元信息机制 | 开发人员能够使用通用的和自己定义的元信息机制来定义资源属性。 |
3 | 超大容量 | 云存储支持0-2T的单文件数据容量,同一时候对object的个数没有限制;云存储提供superfile接口,能够实现2T超大文件的上传和下载。 |
4 | 分片上传、断点下载功能。 | 该功能在网络不稳定的环境下具有很好的表现。 |
5 | Restful风格的HTTP接口和多语言SDK及工具,可极大提高开发人员的开发效率。 | 了解RESTful的相关信息,可參考http://en.wikipedia.org/wiki/Representational_State_Transfer。 |
6 | 签名认证方案 | 基于公钥和密钥的认证方案可适应灵活的业务需求。 |
7 | 强大的ACL权限控制 | 开发人员可通过ACL设置资源的权限(公开或私有),也可授权特定用户具有特定权限。 |
8 | 功能完好的管理平台 | 开发人员可通过管理中心对全部资源进行统一管理。 |
9 | 静态网页托管 | 开发人员可通过将静态页面上传至云存储空间中,并将其訪问权限设置为公开读,就可以实现静态站点的效果。 |
详见:http://developer.baidu.com/wiki/index.php?title=docs/cplat/bcs
本文主要面向百度云存储服务的开发人员,开发人员可通过此文学习怎样使用百度云存储进行开发。
使用百度云存储服务进行开发的详细过程例如以下:
创建Bucket,可通过下面三种方式来实现:
使用云存储服务上传文件有下面四种途径:
云存储bucket能够看成是命名空间,其名称全局唯一,假设某个名称已创建,则其它开发人员将无法再创建同样名称的bucket;开发人员上传的不论什么数据必须是属于某个bucket。上传不论什么数据之前需先确认是否已经创建bucket。 有关bucket的命名限制,请參考#约束与限制。 创建bucket有下面两种方式:
Object是BCS中的基本存储实体,由Key(键)、Data(数据)及MetaData (元数据)组成。
指object的唯一标识,其命名必须以“/”开头,有关object的命名限制,请參考#约束与限制。 如: mystorage/test/我的文件.txt
普通文件的文件大小最大为2GB, 对于更大的超大文件(最大支持2TB)可通过put superfile API来实现。
** * * @param baiduBCS * @param bucket 仓库名 * @param object 上传到仓库中的对象 * @param destFile 本地须要上传的文件对象 * @param contentType 文件类型 */ public static void putObjectByFile(BaiduBCS baiduBCS,String bucket,String object,File destFile,String contentType) { PutObjectRequest request = new PutObjectRequest(bucket, object, destFile); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(contentType); request.setMetadata(metadata); BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request); ObjectMetadata objectMetadata = response.getResult(); log.info("x-bs-request-id: " + response.getRequestId()); log.info(objectMetadata); }
/** * 改动文件的权限 * @param baiduBCS 存储上下文 * @param bucket 存储仓库 * @param object 文件对象 * @param acl 控制权限 */ public static void putObjectPolicyByX_BS_ACL(BaiduBCS baiduBCS,String bucket ,String object,X_BS_ACL acl) { baiduBCS.putObjectPolicy(bucket, object, acl); }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>baiduBCS</groupId> <artifactId>baiduBCS</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>net.sf.flexjson</groupId> <artifactId>flexjson</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.8</version> </dependency> </dependencies> </project>
import com.baidu.inf.iis.bcs.BaiduBCS; import com.baidu.inf.iis.bcs.model.X_BS_ACL; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Properties; /** * Created with IntelliJ IDEA. * User: liu.xing * Date: 14-1-20 * Time: 下午7:23 * coding for fun and coding my life! */ public class MultipUpload { static String filePrefix = "E:/网站/innos"; static int len = filePrefix.length(); private static ArrayList filelist = new ArrayList(); static String bucket = "innosrom"; //文件仓库名 static String bcsDir = "/webDesgin/01/"; //文件存放文件夹 static String object = ""; //文件对象本身 static BaiduBCS bcs; static Properties pro; public static void main(String[] args) { MultipUpload mu = new MultipUpload(); long a = System.currentTimeMillis(); pro = mu.loadFileContentType(); //加载文件格式相应的文档类型 bcs = Sample.init(); //初始化百度云存储环境 mu.refreshFileList(filePrefix); //遍历文件 System.out.println(System.currentTimeMillis() - a); } /** * 遍历文件 * @param strPath 文件夹路径 */ public void refreshFileList(String strPath) { File dir = new File(strPath); File[] files = dir.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { String absPath = files[i].getAbsolutePath(); if (files[i].isDirectory()) { refreshFileList(absPath); } else { String strFileName = absPath.replace("\\", "/"); object = bcsDir + strFileName.substring(len + 1); String contentType = getContentType(object,pro); System.out.println(object+"\t"+contentType); Sample.putObjectByFile(bcs, bucket, object, new File(absPath),contentType); Sample.putObjectPolicyByX_BS_ACL(bcs, bucket, object, X_BS_ACL.PublicReadWrite); //deleteAll(bcs,object); } } } /** * 依据文件名称获取ContentType * @param object 文件名称 * @param pro contentType-file 相应的 Properties对象 * @return ContentType */ private String getContentType(String object,Properties pro) { if(object.contains(".")){ int index = object.lastIndexOf("."); String suffix = object.substring(index+1); //获取文件后缀 Object contentType = pro.get(suffix); if(contentType != null) return contentType.toString(); } return "text/html"; } public void deleteAll(BaiduBCS bcs,String object){ Sample.deleteObject(bcs,object); } /** * 加载ContentType 文件 * @return */ public Properties loadFileContentType(){ InputStream is = getClass().getClassLoader().getSystemResourceAsStream("contentType.proerties"); Properties pro = new Properties(); try { pro.load(is); } catch (IOException e) { e.printStackTrace(); } return pro; } }
/*************************************************************************** * * Copyright (c) 2012 Baidu.com, Inc. All Rights Reserved * **************************************************************************/ import java.io.*; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import com.baidu.inf.iis.bcs.model.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.baidu.inf.iis.bcs.BaiduBCS; import com.baidu.inf.iis.bcs.auth.BCSCredentials; import com.baidu.inf.iis.bcs.auth.BCSSignCondition; import com.baidu.inf.iis.bcs.http.HttpMethodName; import com.baidu.inf.iis.bcs.policy.Policy; import com.baidu.inf.iis.bcs.policy.PolicyAction; import com.baidu.inf.iis.bcs.policy.PolicyEffect; import com.baidu.inf.iis.bcs.policy.Statement; import com.baidu.inf.iis.bcs.request.CreateBucketRequest; import com.baidu.inf.iis.bcs.request.GenerateUrlRequest; import com.baidu.inf.iis.bcs.request.GetObjectRequest; import com.baidu.inf.iis.bcs.request.ListBucketRequest; import com.baidu.inf.iis.bcs.request.ListObjectRequest; import com.baidu.inf.iis.bcs.request.PutObjectRequest; import com.baidu.inf.iis.bcs.request.PutSuperfileRequest; import com.baidu.inf.iis.bcs.response.BaiduBCSResponse; @SuppressWarnings("unused") public class Sample { private static final Log log = LogFactory.getLog(Sample.class); // ---------------------------------------- static String host = "bcs.duapp.com"; static String accessKey = "您自己的私钥"; static String secretKey = "您自己的密钥"; static String bucket = "innosrom"; // ---------------------------------------- static String object = "/index.jsp"; static File destFile = new File("C:\\BCS\\up.txt"); static File saveFile = new File("C:\\BCS\\download.txt"); public static BaiduBCS init(){ BCSCredentials credentials = new BCSCredentials(accessKey, secretKey); BaiduBCS baiduBCS = new BaiduBCS(credentials, host); // baiduBCS.setDefaultEncoding("GBK"); baiduBCS.setDefaultEncoding("UTF-8"); // Default UTF-8 return baiduBCS; } /** * @param args * @throws java.net.URISyntaxException * @throws java.io.IOException */ public static void main(String[] args) throws URISyntaxException, IOException { // PropertyConfigurator.configure("log4j.properties"); BCSCredentials credentials = new BCSCredentials(accessKey, secretKey); BaiduBCS baiduBCS = new BaiduBCS(credentials, host); // baiduBCS.setDefaultEncoding("GBK"); baiduBCS.setDefaultEncoding("UTF-8"); // Default UTF-8 try { // -------------bucket------------- // listBucket(baiduBCS); // createBucket(baiduBCS); // deleteBucket(baiduBCS); // getBucketPolicy(baiduBCS); // putBucketPolicyByPolicy(baiduBCS); // putBucketPolicyByX_BS_ACL(baiduBCS, X_BS_ACL.PublicControl); // listObject(baiduBCS); // ------------object------------- //putObjectByFile(baiduBCS); //putObjectByInputStream(baiduBCS); //getObjectWithDestFile(baiduBCS); // putSuperfile(baiduBCS); //deleteObject(baiduBCS); //getObjectMetadata(baiduBCS); // setObjectMetadata(baiduBCS); // copyObject(baiduBCS, bucket, object + "_copy" + // (System.currentTimeMillis())); //getObjectPolicy(baiduBCS); //putObjectPolicyByPolicy(baiduBCS); putObjectPolicyByX_BS_ACL(baiduBCS, X_BS_ACL.PublicReadWrite); // ------------common------------------ //generateUrl( baiduBCS); } catch (BCSServiceException e) { log.warn("Bcs return:" + e.getBcsErrorCode() + ", " + e.getBcsErrorMessage() + ", RequestId=" + e.getRequestId()); } catch (BCSClientException e) { e.printStackTrace(); } } public static void generateUrl(BaiduBCS baiduBCS) { GenerateUrlRequest generateUrlRequest = new GenerateUrlRequest(HttpMethodName.GET, bucket, object); generateUrlRequest.setBcsSignCondition(new BCSSignCondition()); generateUrlRequest.getBcsSignCondition().setIp("*"); //generateUrlRequest.getBcsSignCondition().setTime(123455L); //generateUrlRequest.getBcsSignCondition().setSize(123455L); System.out.println(baiduBCS.generateUrl(generateUrlRequest)); } public static void copyObject(BaiduBCS baiduBCS, String destBucket, String destObject) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/jpeg"); baiduBCS.copyObject(new Resource(bucket, object), new Resource(destBucket, destObject), objectMetadata); baiduBCS.copyObject(new Resource(bucket, object), new Resource(destBucket, destObject), null); baiduBCS.copyObject(new Resource(bucket, object), new Resource(destBucket, destObject)); } private static void createBucket(BaiduBCS baiduBCS) { // baiduBCS.createBucket(bucket); baiduBCS.createBucket(new CreateBucketRequest(bucket, X_BS_ACL.PublicRead)); } private static void deleteBucket(BaiduBCS baiduBCS) { baiduBCS.deleteBucket(bucket); } public static void deleteObject(BaiduBCS baiduBCS,String object) { Empty result = baiduBCS.deleteObject(bucket, object).getResult(); log.info(result); } private static void getBucketPolicy(BaiduBCS baiduBCS) { BaiduBCSResponse<Policy> response = baiduBCS.getBucketPolicy(bucket); log.info("After analyze: " + response.getResult().toJson()); log.info("Origianal str: " + response.getResult().getOriginalJsonStr()); } public static void getObjectMetadata(BaiduBCS baiduBCS) { ObjectMetadata objectMetadata = baiduBCS.getObjectMetadata(bucket, object).getResult(); log.info(objectMetadata); } private static void getObjectPolicy(BaiduBCS baiduBCS) { BaiduBCSResponse<Policy> response = baiduBCS.getObjectPolicy(bucket, object); log.info("After analyze: " + response.getResult().toJson()); log.info("Origianal str: " + response.getResult().getOriginalJsonStr()); } private static void getObjectWithDestFile(BaiduBCS baiduBCS) { GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, object); baiduBCS.getObject(getObjectRequest, saveFile); } private static void listBucket(BaiduBCS baiduBCS) { ListBucketRequest listBucketRequest = new ListBucketRequest(); BaiduBCSResponse<List<BucketSummary>> response = baiduBCS.listBucket(listBucketRequest); for (BucketSummary bucket : response.getResult()) { log.info(bucket); } } private static void listObject(BaiduBCS baiduBCS) { ListObjectRequest listObjectRequest = new ListObjectRequest(bucket); listObjectRequest.setStart(0); listObjectRequest.setLimit(20); // ------------------by dir { // prefix must start with ‘/‘ and end with ‘/‘ // listObjectRequest.setPrefix("/1/"); // listObjectRequest.setListModel(2); } // ------------------only object { // prefix must start with ‘/‘ // listObjectRequest.setPrefix("/1/"); } BaiduBCSResponse<ObjectListing> response = baiduBCS.listObject(listObjectRequest); log.info("we get [" + response.getResult().getObjectSummaries().size() + "] object record."); for (ObjectSummary os : response.getResult().getObjectSummaries()) { log.info(os.toString()); } } private static void putBucketPolicyByPolicy(BaiduBCS baiduBCS) { Policy policy = new Policy(); Statement st1 = new Statement(); st1.addAction(PolicyAction.all).addAction(PolicyAction.get_object); st1.addUser("zhengkan").addUser("zhangyong01"); st1.addResource(bucket + "/111").addResource(bucket + "/111"); st1.setEffect(PolicyEffect.allow); policy.addStatements(st1); baiduBCS.putBucketPolicy(bucket, policy); } private static void putBucketPolicyByX_BS_ACL(BaiduBCS baiduBCS, X_BS_ACL acl) { baiduBCS.putBucketPolicy(bucket, acl); } public static void putObjectByFile(BaiduBCS baiduBCS) { PutObjectRequest request = new PutObjectRequest(bucket, object, destFile); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("text/html"); request.setMetadata(metadata); BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request); ObjectMetadata objectMetadata = response.getResult(); log.info("x-bs-request-id: " + response.getRequestId()); log.info(objectMetadata); } /** * * @param baiduBCS * @param bucket 仓库名 * @param object 上传到仓库中的对象 * @param destFile 本地须要上传的文件对象 * @param contentType 文件类型 */ public static void putObjectByFile(BaiduBCS baiduBCS,String bucket,String object,File destFile,String contentType) { PutObjectRequest request = new PutObjectRequest(bucket, object, destFile); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(contentType); request.setMetadata(metadata); BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putObject(request); ObjectMetadata objectMetadata = response.getResult(); log.info("x-bs-request-id: " + response.getRequestId()); log.info(objectMetadata); } public static void putObjectByInputStream(BaiduBCS baiduBCS) throws FileNotFoundException { File file = createSampleFile(); InputStream fileContent = new FileInputStream(file); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("text/html"); objectMetadata.setContentLength(file.length()); PutObjectRequest request = new PutObjectRequest(bucket, object, fileContent, objectMetadata); ObjectMetadata result = baiduBCS.putObject(request).getResult(); log.info(result); } private static void putObjectPolicyByPolicy(BaiduBCS baiduBCS) { Policy policy = new Policy(); Statement st1 = new Statement(); st1.addAction(PolicyAction.all).addAction(PolicyAction.get_object); st1.addUser("zhengkan").addUser("zhangyong01"); st1.addResource(bucket + object).addResource(bucket + object); st1.setEffect(PolicyEffect.allow); policy.addStatements(st1); baiduBCS.putObjectPolicy(bucket, object, policy); } private static void putObjectPolicyByX_BS_ACL(BaiduBCS baiduBCS, X_BS_ACL acl) { baiduBCS.putObjectPolicy(bucket, object, acl); } /** * 改动文件的权限 * @param baiduBCS 存储上下文 * @param bucket 存储仓库 * @param object 文件对象 * @param acl 控制权限 */ public static void putObjectPolicyByX_BS_ACL(BaiduBCS baiduBCS,String bucket ,String object,X_BS_ACL acl) { baiduBCS.putObjectPolicy(bucket, object, acl); } public static void putSuperfile(BaiduBCS baiduBCS) { List<SuperfileSubObject> subObjectList = new ArrayList<SuperfileSubObject>(); // 0 BaiduBCSResponse<ObjectMetadata> response1 = baiduBCS.putObject(bucket, object + "_part0", createSampleFile()); subObjectList.add(new SuperfileSubObject(bucket, object + "_part0", response1.getResult().getETag())); // 1 BaiduBCSResponse<ObjectMetadata> response2 = baiduBCS.putObject(bucket, object + "_part1", createSampleFile()); subObjectList.add(new SuperfileSubObject(bucket, object + "_part1", response2.getResult().getETag())); // put superfile PutSuperfileRequest request = new PutSuperfileRequest(bucket, object + "_superfile", subObjectList); BaiduBCSResponse<ObjectMetadata> response = baiduBCS.putSuperfile(request); ObjectMetadata objectMetadata = response.getResult(); log.info("x-bs-request-id: " + response.getRequestId()); log.info(objectMetadata); } public static void setObjectMetadata(BaiduBCS baiduBCS) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("text/html12"); baiduBCS.setObjectMetadata(bucket, object, objectMetadata); } private static File createSampleFile() { try { File file = File.createTempFile("java-sdk-", ".txt"); file.deleteOnExit(); Writer writer = new OutputStreamWriter(new FileOutputStream(file)); writer.write("01234567890123456789\n"); writer.write("01234567890123456789\n"); writer.write("01234567890123456789\n"); writer.write("01234567890123456789\n"); writer.write("01234567890123456789\n"); writer.close(); return file; } catch (IOException e) { log.error("tmp file create failed."); return null; } } }
百度云存储教程---免费建立自己的静态网站,布布扣,bubuko.com
原文:http://www.cnblogs.com/mengfanrong/p/3741937.html