阿里云对象存储服务 springcloud服务接口
本文章提供的是阿里云对象存储的核心示例代码 , 具体springcloud接口服务于最下面查看 示例下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
package com.aliyun.oss.demo;
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List;
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.BucketInfo; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.OSSObjectSummary; import com.aliyun.oss.model.ObjectListing;
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator;
public class HelloOSS { static Logger logger = Logger.getLogger(HelloOSS.class);
private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
private static String accessKeyId = "<yourAccessKeyId>"; private static String accessKeySecret = "<yourAccessKeySecret>";
private static String bucketName = "<yourBucketName>";
private static String firstKey = "my-first-key";
public static void main(String[] args) {
PropertyConfigurator.configure("conf/log4j.properties");
logger.info("Started");
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
try {
if (ossClient.doesBucketExist(bucketName)) { System.out.println("您已经创建Bucket:" + bucketName + "。"); } else { System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。"); ossClient.createBucket(bucketName); }
BucketInfo info = ossClient.getBucketInfo(bucketName); System.out.println("Bucket " + bucketName + "的信息如下:"); System.out.println("\t数据中心:" + info.getBucket().getLocation()); System.out.println("\t创建时间:" + info.getBucket().getCreationDate()); System.out.println("\t用户标志:" + info.getBucket().getOwner());
InputStream is = new ByteArrayInputStream("Hello OSS".getBytes()); ossClient.putObject(bucketName, firstKey, is); System.out.println("Object:" + firstKey + "存入OSS成功。");
OSSObject ossObject = ossClient.getObject(bucketName, firstKey); InputStream inputStream = ossObject.getObjectContent(); StringBuilder objectContent = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while (true) { String line = reader.readLine(); if (line == null) break; objectContent.append(line); } inputStream.close(); System.out.println("Object:" + firstKey + "的内容是:" + objectContent);
String fileKey = "README.md"; ossClient.putObject(bucketName, fileKey, new File("README.md")); System.out.println("Object:" + fileKey + "存入OSS成功。");
ObjectListing objectListing = ossClient.listObjects(bucketName); List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries(); System.out.println("您有以下Object:"); for (OSSObjectSummary object : objectSummary) { System.out.println("\t" + object.getKey()); }
ossClient.deleteObject(bucketName, firstKey); System.out.println("删除Object:" + firstKey + "成功。"); ossClient.deleteObject(bucketName, fileKey); System.out.println("删除Object:" + fileKey + "成功。");
} catch (OSSException oe) { oe.printStackTrace(); } catch (ClientException ce) { ce.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { ossClient.shutdown(); }
logger.info("Completed"); }
}
|