Getting started with Alibaba Cloud Object Storage OSS

Data and file storage:

  • Data (MySQL)
  • Data (MySQL) + file (local: IO stream)
  • Data (MySQL) + cache (Redis) + files (on server cluster, fastDFS (cluster), Hdfs (big data))
  • Data (MySQL) + cache (Redis) + file (OSS)

OSS is to solve the massive data storage and elastic expansion, we can use Alibaba Cloud OSS.

1. Subscription service (free subscription)

 

 

 

 2. Bucket library

  1. Click to create a bucket
  2. Fill in the bucket name and area
    1. Note: I chose Hangzhou for the area here, because when I bought the server at the time, I chose the Hangzhou area. The OSS monthly resource package is given as a gift. Of course, it can also be charged according to the flow rate. A few dollars can be obtained for personal testing.

 

 3. Note: Use sub-authority-security

  1. Click access control
  2. Create user
  3. Create User Group
  4. User group permission settings
  5. Add user to user group
  6. Save account information

 

 

 

 

 

 

 

 4. Test API

  1. Find the official documentation
  2. test

 

 

 

 

1  package com.coding.aliyun;
 2  
3  import com.aliyun.oss.OSS;
 4  import com.aliyun.oss.OSSClientBuilder;
 5  import com.aliyun.oss.model.CannedAccessControlList;
 6  import com.aliyun.oss.model .GetObjectRequest;
 7  import com.aliyun.oss.model.ObjectMetadata;
 8  import org.junit.Test;
 9  
10  import java.io. * ;
 11  
12  // Please refer to the official documentation for detailed steps, please refer to the official documentation for detailed steps, For detailed steps, please refer to the official document 
13  public  class OSSTest {
 14      //Endpoint takes Hangzhou as an example. Please fill in other regions according to the actual situation. 
15      String endpoint = "http://oss-cn-hangzhou.aliyuncs.com" ;
 16      // The Alibaba Cloud main account AccessKey has access to all APIs, and the risk is very high. It is strongly recommended that you create and use a RAM account for API access or daily operation and maintenance. Please log in to https://ram.console.aliyun.com to create a RAM account. 
17      String accessKeyId = "LTAI4Fcy4XxxxxxQR4" ;
 18      String accessKeySecret = "spI0CUXTDxxxxxxxYsf4lmIaUdrIu" ;
 19      String bucketName = "vin955" ;
 20  
23      @Test
 24      public  void testCreate () {
 25          // Create an OSSClient instance. 
26          OSS ossClient = newOSSClientBuilder (). Build (endpoint, accessKeyId, accessKeySecret);
 27  
28          boolean exists = ossClient.doesBucketExist (bucketName);
 29  
30          if (exists == false ) { // Determine whether this storage space exists
 31              // Create storage space. 
32              ossClient.createBucket (bucketName);
 33          }
 34  
35          // Set the access permission of the storage space to private. 
36          ossClient.setBucketAcl (bucketName, CannedAccessControlList.PublicRead);
 37  
38          // Close OSSClient. 
39          ossClient.shutdown ();
 40      }
41 
42     @Test
43     public void testCreate2(){
44         // 创建OSSClient实例。
45         OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
46 
47         // 上传文件流。
48         InputStream inputStream = null;
49         try {
50             inputStream = new FileInputStream("C:\\Users\\heng\\Desktop\\ava.png");
51         } catch (FileNotFoundException e) {
52             e.printStackTrace();
53         }
54 
55         ossClient.putObject(bucketName, "ava/aaa.png", inputStream);
56 
57         // 关闭OSSClient。
58         ossClient.shutdown();
59     }
60 
63     @Test
64     public void testCreate3() throws IOException {
65         // 创建OSSClient实例。
66         OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
67 
68         String objectName = "ava/aaa.png";
69 
70         //ossObject contains the name of the storage space where the file is located, the file name, file meta information, and an input stream.
71  //         OSSObject ossObject = ossClient.getObject (bucketName, objectName); 
72          ObjectMetadata ossObject = ossClient.getObject ( new GetObjectRequest (bucketName, objectName), new File ("a.png" ));
 73  
74  
75          // Close OSSClient. 
76          ossClient.shutdown ();
 77      }
 78  
79  
80      @Test
 81      public  void testCreate4 () throws IOException {
 82          // Create an OSSClient instance. 
83         OSS ossClient = new OSSClientBuilder (). Build (endpoint, accessKeyId, accessKeySecret);
 84  
85  
86          String objectName = "ava / aaa.png" ;
 87          // Delete the file. To delete a folder, set ObjectName to the corresponding folder name. If the folder is not empty, you need to delete all objects under the folder to delete the folder. 
88          ossClient.deleteObject (bucketName, objectName);
 89  
90          ossClient.shutdown ();
 91      }
 92  
93 }

 

Guess you like

Origin www.cnblogs.com/ShallowPen/p/12717243.html