oss storage usage

Alibaba Cloud provides 5 ways to use oss storage, including console, command line, graphical management tool ossbrowser, SDK, and REST API; here are some descriptions of the commonly used command line and javasdk, and the others are not detailed one by one. Note, if you are interested, you can go to the official website to see related documents and guidance videos

Official website document address: How to use OSS console, ossutil, ossbrowser, OSSSDK_Object Storage-Alibaba Cloud Help Center

1. Console:

You can complete common basic operations through the OSS console, such as creating a storage space (Bucket), uploading and downloading files (Object), and so on.

The flowchart for using the OSS console is as follows:

  1. Activate OSS service
  2. create storage space
  3. upload files

After the file is uploaded to the target Bucket, you can perform the following operations:

  • Download the file locally. For details, see download file.
  • Share files with third parties by generating signed URL s for download or preview. For details, see share files.
  • To access files through a custom domain name (own domain name), you need to bind the custom domain name to the Bucket where the file is located. For details, see Use your own domain name to access OSS resources.  

Two, the command line

Five steps are required:

1. Install ossuntil

Download and install:

Operating environment: Windows, Linux, macOS

         Supported architecture: x86 (32bit, 64bit), ARM (32bit, 64bit

Download link: ossutil1.7.8 version is compiled based on Golang 1.16 version, and does not support using ossutil through macOS x86 32bit operating system. Therefore, ossutil1.7.8 and above versions no longer provide the ossutilmac32 installation package.

Download the corresponding version according to your operating system, and then follow the steps below to install and configure ossutil. The following uses a 64-bit system as an example.

Installation: (Take linux as an example, see the official website documentation for others)

        

2. Linux system installation

  1. Run the following command to download ossutil.
    wget https://gosspublic.alicdn.com/ossutil/1.7.14/ossutil64                           

    Note When copying the download address, the content of spm encoding will be added automatically. When using the wget command to download, please delete the content of ?spm=xxxx in the link.

  2. Run the following command to modify the file execution permission.
    chmod 755 ossutil64
  3. Generate configuration files using interactive configure.
    1. Enter configuration commands.
      ./ossutil64 config
    2. Set the configuration file path according to the prompt. It is recommended to directly press Enter to use the path of the default configuration file.
      Please enter the configuration file name, the file name can have a path (the default is:/home/user/.ossutilconfig,Enter will use the default path.
      If the user sets it to another path, you need to use the command--config-file option is set to that path): 

      ossutil uses /home/user/.ossutilconfig as the configuration file by default. If you set the path of the configuration file, you need to add the -c option to specify the configuration file every time you use the command. For example, the configuration file is saved as /home/config. When using ls, the command format is as follows:

      ./ossutil64 ls oss://examplebucket -c /home/config
    3. Follow the prompts to set the language of the tool.

      Please enter the language CH or EN. The language used by the tool is consistent with the operating system by default. This configuration item will take effect after the config command is set successfully.

2. Create a storage space bucket

  • command format
    ./ossutil64 mb oss://bucket
  • Example of use

    Create a bucket named examplebucket.

    ./ossutil64 mb oss://examplebucket

    The following output shows that the examplebucket has been successfully created.

    0.668238(s) elapsed

For more examples of creating Bucket s, see mb (create storage space).

3. Upload files

  • command format
    ./ossutil64 cp local_file oss://bucket
  • Example of use
    • Upload a single file examplefile.txt to the target storage space examplebucket.
      ./ossutil64 cp examplefile.txt oss://examplebucket
    • Upload a single file examplefile.txt to the target storage space examplebucket, and rename the file to exampleobject.txt.
      ./ossutil64 cp examplefile.txt oss://examplebucket/exampleobject.txt

    The following output indicates that the file has been successfully uploaded to the target Bucket.

    0.720812(s) elapsed

For more examples of uploading files, see cp (upload file).

4. Download files

  • command format
    ./ossutil64 cp cloud_url local_file
  • Example of use

    Download the file examplefile.txt from the target storage space examplebucket to the local local folder.

    ./ossutil64 cp oss://examplebucket/examplefile.txt localfolder/

    Download the file examplefile.txt from the target storage space examplebucket to the local local folder, and rename the file to exampleobject.txt.

    ./ossutil64 cp oss://examplebucket/examplefile.txt localfolder/exampleobject.txt

    The following output shows that the file was successfully downloaded to the local target folder.

    0.720812(s) elapsed

For more examples of downloading files, see cp (download file).

5. Share files

Generate signed URL

  • command format
    ./ossutil64 sign cloud_url [--timeout <value>]
  • Example of use

    Generate a file signature URL with a timeout of 3600 seconds for the target file oss://examplebucket/exampleobject.txt.

    ./ossutil64 sign oss://examplebucket/exampleobject.txt --timeout 3600 

    The following output shows that the file signing URL was successfully generated.

    https://examplebucket.oss-cn-hangzhou.aliyuncs.com/exampleobject.txt?Expires=1608282224&OSSAccessKeyId=LTAI4G33piUmgRN1DXx9****&Signature=jo4%2FGykfuc1A4fvyvKRpRyymYH****
    0.368676(s) elapsed

3. Graphical management tool ossbrowser

4. JAVASDK

1. Example project

OSS Java SDK provides sample projects based on Maven and Ant. You can compile and run the sample project on your local device, or develop your application based on the sample project. For how to compile and run the project, please refer to README.md in the project directory.

2. Create storage space

Storage space is the global namespace of OSS, which is equivalent to a data container and can store several files.

The following code is used to create storage space.

public class Demo {

    public static void main(String[] args) throws Exception {
         // Endpoint takes East China 1 (Hangzhou) as an example, and other Region s should be filled in according to the actual situation.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Alibaba Cloud account AccessKey has access to all API s, which is very risky. It is strongly recommended that you create and use a RAM user for API access or daily operation and maintenance. Please log in to the RAM console to create a RAM user.
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Fill in the Bucket name, such as examplebucket.
        String bucketName = "examplebucket";

        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // Create a storage space.
            ossClient.createBucket(bucketName);

        } 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 {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3. Upload files

The following code is used to upload files to OSS through streaming upload.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.ByteArrayInputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint takes East China 1 (Hangzhou) as an example, and other Region s should be filled in according to the actual situation.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Alibaba Cloud account AccessKey has access to all API s, which is very risky. It is strongly recommended that you create and use a RAM user for API access or daily operation and maintenance. Please log in to the RAM console to create a RAM user.
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Fill in the Bucket name, such as examplebucket.
        String bucketName = "examplebucket";
        // Fill in the complete path of the Object, such as exampledir/exampleobject.txt.  The Bucket name cannot be included in the full Object path.
        String objectName = "exampledir/exampleobject.txt";

        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            String content = "Hello OSS";
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
        } 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 {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3. Download files

The following code is used to download files from OSS through streaming download.

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.OSSObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint takes East China 1 (Hangzhou) as an example, and other Region s should be filled in according to the actual situation.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Alibaba Cloud account AccessKey has access to all API s, which is very risky. It is strongly recommended that you create and use a RAM user for API access or daily operation and maintenance. Please log in to the RAM console to create a RAM user.
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Fill in the Bucket name, such as examplebucket.
        String bucketName = "examplebucket";
        // Fill in the complete path of the Object, such as exampledir/exampleobject.txt.  The Bucket name cannot be included in the full Object path.
        String objectName = "exampledir/exampleobject.txt";

        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // Call ossClient.getObject to return an OSSObject instance, which contains file content and file meta information.
            OSSObject ossObject = ossClient.getObject(bucketName, objectName);
            // Call ossObject.getObjectContent to get the file input stream, which can be read to get its content.
            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);
                }
                // After the data is read, the obtained stream must be closed, otherwise it will cause a connection leak, resulting in no connection available for the request, and the program cannot work normally.
                content.close();
            }
        } 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 {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3. List files

The following code is used to list the files under the examplebucket storage space. By default, 100 files are listed.

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.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint takes East China 1 (Hangzhou) as an example, and other Region s should be filled in according to the actual situation.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Alibaba Cloud account AccessKey has access to all API s, which is very risky. It is strongly recommended that you create and use a RAM user for API access or daily operation and maintenance. Please log in to the RAM console to create a RAM user.
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Fill in the Bucket name, such as examplebucket.
        String bucketName = "examplebucket";

        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // ossClient.listObjects returns an ObjectListing instance, including the return result of this listObject request.
            ObjectListing objectListing = ossClient.listObjects(bucketName);
            // objectListing.getObjectSummaries Get the description information of all files.
            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() + ")");
            }
        } 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 {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

5. REST API: slightly

Tags: Java Database

Posted by newbie9999 on Wed, 14 Dec 2022 07:42:55 +0300