|
|
@@ -1,11 +1,10 @@
|
|
|
+package cn.reghao.devops.manager.aliyun;
|
|
|
+
|
|
|
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.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
@@ -16,17 +15,17 @@ import com.aliyun.oss.model.*;
|
|
|
* @date 2023-12-18 16:50:50
|
|
|
*/
|
|
|
public class AliyunOss {
|
|
|
- private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
|
|
|
- private static String accessKeyId = "jYk8lOKwSCorFEz7";
|
|
|
- private static String accessKeySecret = "hzSrcew08V5zk58kVVgInV7OqbHyVc";
|
|
|
- private static String bucketName = "iquizoo";
|
|
|
- private static String objectNamePrefix = "dev/1/";
|
|
|
- private static OSS ossClient;
|
|
|
- static {
|
|
|
- ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ private final String bucketName = "iquizoo";
|
|
|
+ private final OSS ossClient;
|
|
|
+
|
|
|
+ public AliyunOss() {
|
|
|
+ String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ String accessKeyId = "jYk8lOKwSCorFEz7";
|
|
|
+ String accessKeySecret = "hzSrcew08V5zk58kVVgInV7OqbHyVc";
|
|
|
+ this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
}
|
|
|
|
|
|
- static void uploadObject(String objectName, File file) throws IOException {
|
|
|
+ public void uploadObject(String objectName, File file) throws IOException {
|
|
|
System.out.println("Uploading a new object to OSS from a file\n");
|
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);
|
|
|
ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
@@ -35,18 +34,31 @@ public class AliyunOss {
|
|
|
ossClient.putObject(putObjectRequest);
|
|
|
}
|
|
|
|
|
|
- static void downloadObject(String objectName) throws IOException {
|
|
|
+ public void downloadObject(String objectName) throws IOException {
|
|
|
System.out.println("Downloading an object");
|
|
|
OSSObject object = ossClient.getObject(bucketName, objectName);
|
|
|
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
|
|
|
displayTextInputStream(object.getObjectContent());
|
|
|
}
|
|
|
|
|
|
- static void deleteObject(String objectName) {
|
|
|
+ private void displayTextInputStream(InputStream input) throws IOException {
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
|
|
+ while (true) {
|
|
|
+ String line = reader.readLine();
|
|
|
+ if (line == null) break;
|
|
|
+
|
|
|
+ System.out.println(" " + line);
|
|
|
+ }
|
|
|
+ System.out.println();
|
|
|
+
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteObject(String objectName) {
|
|
|
ossClient.deleteObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
- static void listObjects(String prefix) {
|
|
|
+ public void listObjects(String prefix) {
|
|
|
System.out.println("Listing objects");
|
|
|
ObjectListing objectListing = ossClient.listObjects(bucketName, prefix);
|
|
|
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
|
|
@@ -61,10 +73,14 @@ public class AliyunOss {
|
|
|
System.out.println();
|
|
|
}
|
|
|
|
|
|
+ public void close() {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
String accessKeyId = System.getenv("ACCESS_KEY_ID");
|
|
|
String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
|
|
|
- System.out.println();
|
|
|
+ AliyunOss aliyunOss = new AliyunOss();
|
|
|
|
|
|
/*String baseDir = "/home/reghao/data/image/";
|
|
|
File dir = new File(baseDir);
|
|
|
@@ -83,34 +99,8 @@ public class AliyunOss {
|
|
|
|
|
|
/*String objectName = "dev/test/1.txt";
|
|
|
File file = createSampleFile();*/
|
|
|
- listObjects(objectNamePrefix);
|
|
|
- }
|
|
|
-
|
|
|
- static void close() {
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
-
|
|
|
- private static File createSampleFile() throws IOException {
|
|
|
- File file = File.createTempFile("oss-java-sdk-", ".txt");
|
|
|
- file.deleteOnExit();
|
|
|
|
|
|
- Writer writer = new OutputStreamWriter(new FileOutputStream(file));
|
|
|
- writer.write("abcdefghijklmnopqrstuvwxyz\n");
|
|
|
- writer.write("0123456789011234567890\n");
|
|
|
- writer.close();
|
|
|
- return file;
|
|
|
- }
|
|
|
-
|
|
|
- private static void displayTextInputStream(InputStream input) throws IOException {
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
|
|
- while (true) {
|
|
|
- String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
-
|
|
|
- System.out.println(" " + line);
|
|
|
- }
|
|
|
- System.out.println();
|
|
|
-
|
|
|
- reader.close();
|
|
|
+ String objectNamePrefix = "dev/1/";
|
|
|
+ aliyunOss.listObjects(objectNamePrefix);
|
|
|
}
|
|
|
}
|