|
@@ -22,25 +22,16 @@ package edu.nju.util;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
-import java.io.OutputStreamWriter;
|
|
|
-import java.io.Writer;
|
|
|
|
|
|
import com.aliyun.oss.ClientException;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.OSSException;
|
|
|
-import com.aliyun.oss.model.Bucket;
|
|
|
import com.aliyun.oss.model.CannedAccessControlList;
|
|
|
import com.aliyun.oss.model.CreateBucketRequest;
|
|
|
-import com.aliyun.oss.model.ListBucketsRequest;
|
|
|
-import com.aliyun.oss.model.OSSObject;
|
|
|
-import com.aliyun.oss.model.OSSObjectSummary;
|
|
|
-import com.aliyun.oss.model.ObjectAcl;
|
|
|
-import com.aliyun.oss.model.ObjectListing;
|
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
|
|
|
|
/**
|
|
@@ -49,16 +40,59 @@ import com.aliyun.oss.model.PutObjectRequest;
|
|
|
*/
|
|
|
public class OssAliyun {
|
|
|
|
|
|
- private static String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
|
|
|
+ private static String shangHaiEndpoint = "http://oss-cn-shanghai.aliyuncs.com";
|
|
|
+ private static String hangZhouEndpoint = "http://oss-cn-hangzhou.aliyuncs.com";
|
|
|
private static String accessKeyId = "LTAI4FdrT3HsfdR5edBVN7ws";
|
|
|
private static String accessKeySecret = "yroxrpm46DzTyzHrLBZzS3MRNIicP6";
|
|
|
private static String bucketName = "mooctest-site";
|
|
|
|
|
|
+ public static OSS initHangZhouOss(){
|
|
|
+ return new OSSClientBuilder().build(hangZhouEndpoint, accessKeyId, accessKeySecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void uploadFile(OSS ossClient,String bucketName,String objectName,File file){
|
|
|
+ try {
|
|
|
+ /*
|
|
|
+ * Determine whether the bucket exists
|
|
|
+ */
|
|
|
+ if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
+ /*
|
|
|
+ * Create a new OSS bucket
|
|
|
+ */
|
|
|
+ ossClient.createBucket(bucketName);
|
|
|
+ CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
|
|
|
+ createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
|
|
+ ossClient.createBucket(createBucketRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Upload an object to your bucket
|
|
|
+ */
|
|
|
+ System.out.println("Uploading a new object to OSS from a file\n");
|
|
|
+ ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ System.out.println("Error Message: " + oe.getErrorMessage());
|
|
|
+ System.out.println("Error Code: " + oe.getErrorCode());
|
|
|
+ System.out.println("Request ID: " + oe.getRequestId());
|
|
|
+ System.out.println("Host ID: " + oe.getHostId());
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ System.out.println("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.");
|
|
|
+ System.out.println("Error Message: " + ce.getMessage());
|
|
|
+ } finally {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
public static void uploadFile(String objectName,File file) throws IOException {
|
|
|
/*
|
|
|
* Constructs a client instance with your account for accessing OSS
|
|
|
*/
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(shangHaiEndpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
|
/*
|
|
|
* Determine whether the bucket exists
|
|
@@ -137,7 +171,7 @@ public class OssAliyun {
|
|
|
/*
|
|
|
* Constructs a client instance with your account for accessing OSS
|
|
|
*/
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(shangHaiEndpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
|
/*
|
|
|
* Determine whether the bucket exists
|
|
@@ -181,7 +215,9 @@ public class OssAliyun {
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
|
|
while (true) {
|
|
|
String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
+ if (line == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
System.out.println(" " + line);
|
|
|
}
|
|
@@ -191,7 +227,7 @@ public class OssAliyun {
|
|
|
}
|
|
|
|
|
|
public static void main(String[]arg){
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(shangHaiEndpoint, accessKeyId, accessKeySecret);
|
|
|
String objectName="paperjson/1492-2612.json";
|
|
|
try {
|
|
|
/*
|