|
|
@@ -0,0 +1,116 @@
|
|
|
+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;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @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);
|
|
|
+ }
|
|
|
+
|
|
|
+ static 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();
|
|
|
+ objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
|
|
|
+ putObjectRequest.setMetadata(objectMetadata);
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ static 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) {
|
|
|
+ ossClient.deleteObject(bucketName, objectName);
|
|
|
+ }
|
|
|
+
|
|
|
+ static void listObjects(String prefix) {
|
|
|
+ System.out.println("Listing objects");
|
|
|
+ ObjectListing objectListing = ossClient.listObjects(bucketName, prefix);
|
|
|
+ for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
|
|
+ String objectName = objectSummary.getKey();
|
|
|
+ long size = objectSummary.getSize();
|
|
|
+ if (size != 0) {
|
|
|
+ deleteObject(objectName);
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")");
|
|
|
+ }
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
+
|
|
|
+ /*String baseDir = "/home/reghao/data/image/";
|
|
|
+ File dir = new File(baseDir);
|
|
|
+ for (File file : dir.listFiles()) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String filename = file.getName();
|
|
|
+ String objectName = String.format("%s%s", objectNamePrefix, filename);
|
|
|
+ uploadObject(objectName, file);
|
|
|
+
|
|
|
+ String url = String.format("https://static.cdn.iquizoo.com/%s", objectName);
|
|
|
+ System.out.println("url: " + url);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /*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();
|
|
|
+ }
|
|
|
+}
|