Getting Started with Alibaba Cloud OSS Storage

1. Activate the Object Storage OSS service

The steps to activate the object storage OSS service are as follows: apply for an Alibaba Cloud account -> real-name authentication -> activate the object storage OSS service -> enter the management console.
insert image description here

2. Console operation

(1) First create a Bucket
insert image description here
bucket name: subtle-chat, set read and write permissions for public read.
insert image description here

(2) Create a new directory for storing different types of files
insert image description here

3. Create RAM sub-user

(1) Enter the sub-user management page

insert image description here

insert image description here

(2) Add a user and
enter the RAM access control background to create a new user
insert image description here
(3) Obtain the sub-user key

After completing the user creation, obtain the AccessKeyId and AccessKeySecret, and save them. Because it cannot be obtained again after closing the interface.
insert image description here
(4) Set user permissions

Set user permissions and add the permission of AliyunOSSFullAccess (Manage Object Storage Service (OSS) permissions)

insert image description here

insert image description here

4. SDK learning

Enter the OSS learning path interface: https://help.aliyun.com/learn/learningpath/oss.html
insert image description here

4.1 Create a test project

Create a Maven project, configure the pom, and add the following dependencies:

    <dependencies>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

4.2 Test use

4.2.1 Create Bucket

public class OSSTest {
    
    

    // Endpoint以杭州为例,其它Region请按实际情况填写。
    String endpoint = "ttp://oss-cn-beijing.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
    String accessKeyId = "your accessKeyId";
    String accessKeySecret = "your accessKeySecret";
    String bucketName = "subtle-chat";

    @Test
    public void testCreateBucket() {
    
    
        
 		// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建CreateBucketRequest对象。
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);

        // 创建存储空间。
        ossClient.createBucket(createBucketRequest);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
}

4.2.2 Determining whether a bucket exists

@Test
public void testExist() {
    
    

    // 创建OSSClient实例。
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    boolean exists = ossClient.doesBucketExist(bucketName);
    System.out.println(exists);

    // 关闭OSSClient。
    ossClient.shutdown();
}

4.2.3 Setting Bucket Access Permission

@Test
public void testAccessControl() {
    
    

    // 创建OSSClient实例。
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    // 设置存储空间的访问权限为:公共读。
    ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);

    // 关闭OSSClient。
    ossClient.shutdown();
}

4.2.4 File Management

Upload file demo code:

    @Test
    public void testFileUpload() {
    
    
        // 填写文件名。文件名包含路径,不包含Bucket名称。例如exampledir/exampleobject.txt。
        //group-chat对应bucket中的目录名
        String objectName = "group-chat/exampleobject.txt";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
    
    

            String content = "Hello OSS";
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
        } catch (OSSException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }

Download file demo code:

   @Test
    public void testFileDownload() {
    
    
        // 填写文件名。文件名包含路径,不包含Bucket名称。例如exampledir/exampleobject.txt。
        //group-chat对应bucket中的目录名
        String objectName = "group-chat/exampleobject.txt";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
    
    

            // 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。
            OSSObject ossObject = ossClient.getObject(bucketName, objectName);
            // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
            InputStream content = ossObject.getObjectContent();
            if (content != null) {
    
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                while (true) {
    
    
                    String line = reader.readLine();
                    if (line == null) break;
                    System.out.println("\n" + line);
                }
                // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
                content.close();
            }
        } catch (OSSException | IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }

Results of the:
insert image description here

Delete the file demo code:

    @Test
    public void testFileDelete() {
    
    
        // 填写文件名。文件名包含路径,不包含Bucket名称。例如exampledir/exampleobject.txt。
        //group-chat对应bucket中的目录名
        String objectName = "group-chat/exampleobject.txt";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
    
    

            // 删除文件。
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException e){
    
    
            e.printStackTrace();
        } finally {
    
    
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }

Learn more: https://help.aliyun.com/document_detail/32007.html

Guess you like

Origin blog.csdn.net/huangjhai/article/details/121732827