This is my second article about getting started

preface

The Object Storage Service (OSS) is a massive, secure, low-cost, and highly reliable cloud Storage Service suitable for storing files of any type. Flexible expansion of capacity and processing capability, multiple storage types for selection, comprehensive optimization of storage costs.

What is object storage

Object Storage Service (OSS) is a massive, secure, low-cost, and durable cloud Storage Service.

OSS characteristics

  • persistence
  • security
  • The cost is low
  • Intelligent store
  • Convenient and quick
  • Powerful and flexible
  • Data redundancy mechanism
  • Rich and powerful processing functions

2. Application scenarios

1. Mass storage of pictures, audio and video applications

OSS can be used to store massive files such as pictures, audio and video files, and logs. Terminal devices, Web applications, and mobile applications can directly write or read data to OSS. OSS supports streaming write and file write.

2. Separation of static and dynamic resources from web or mobile applications

Using massive Internet bandwidth, OSS enables concurrent downloading of massive data over the Internet. OSS provides native transmission acceleration functions, including upload acceleration and download acceleration, to improve transnational and trans-oceanic data upload and download experience. At the same time, OSS can also combine with CDN products to provide a solution for storing and distributing static content to edge nodes. By using CDN edge nodes’ cached data, OSS can improve the experience of the same file being downloaded repeatedly and concurrently by customers in the same region.

3. Cloud data processing

After a file is uploaded to the OSS, it can be used with the media processing service and image processing service for cloud data processing.

Three, quick use

1. Method 1: Add dependencies to Maven project (recommended)

To use the OSS Java SDK in Maven projects, simply add the corresponding dependency to pom.xml. For example, in <dependencies>, add the following information:

< the dependency > < groupId > com. Aliyun. Oss < / groupId > < artifactId > aliyun - SDK - oss < / artifactId > < version > 3.10.2 < / version > </dependency>Copy the code

If you are using Java 9 or later, you need to add JAXB-related dependencies. Example code for adding JAXB-related dependencies is as follows:

<dependency> <groupId>javax.xml.bind</groupId> <artifactId> JAXB-API </artifactId> <version>2.3.1</version> </dependency> < the dependency > < groupId > javax.mail. Activation < / groupId > < artifactId > activation < / artifactId > < version > 1.1.1 < / version > </dependency> <! -- no more than 2.3.3--> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId> The < version > 2.3.3 < / version > < / dependency >Copy the code

2. Method 2: Import JAR package in Eclipse project

Take 3.10.2 as an example, the procedure is as follows:

  1. Download the Java SDK development package.
  2. Decompress the development package.
  3. Copy the aliyun-sdK-osS-3.10.2. jar file from the extracted folder and all files in the lib folder into your project.
  4. Select your project in Eclipse, right-click Properties > Java Build Path > Add JARs.
  5. Select all the JAR files to be copied and import them to the Libraries.

3. Method 3: Import JAR package in Intellij IDEA project

Take 3.10.2 as an example, the procedure is as follows:

  1. Download the Java SDK development package.
  2. Decompress the development package.
  3. Copy the aliyun-sdK-osS-3.10.2. jar file from the extracted folder and all the JAR files in the lib folder into your project.
  4. Select your Project in Intellij IDEA, right-click and choose File > Project Structure > Modules > Dependencies > + > JARs or Directories.
  5. Select all the JAR files to be copied and import them to the External Libraries.

4. Use examples

1. Upload files

 public static void uploadFile(File file, String goodsImgPath) {
        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // Upload the file. 
      
        consists of the local file path plus the file name and suffix, for example, /users/local/myfile.txt.
      
        ossClient.putObject(bucketName, goodsImgPath, file);
        ossClient.setObjectAcl(bucketName, goodsImgPath, CannedAccessControlList.PublicRead);
        // Close OSSClient.
        ossClient.shutdown();
    }
Copy the code

2. Upload the file stream

  public static void uploadFileInputStream(InputStream inputStream , String goodsImgPath) {
        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // Upload the file stream.
        ossClient.putObject(bucketName, goodsImgPath, inputStream);
        ossClient.setObjectAcl(bucketName, goodsImgPath, CannedAccessControlList.PublicRead);
        // Close OSSClient.
        ossClient.shutdown();
    }
Copy the code

3. Download files

  public static void downloadFileInputStream(InputStream inputStream , String goodsImgPath) {
        // Create an OSSClient instance.
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        OssObject contains the name of the storage space where the file resides, the file name, the file meta-information, and an input stream.
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);

        // Read the contents of the file.
        System.out.println("Object content:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("\n" + line);
        }
        // Close the stream.
        reader.close();

        // Close OSSClient.
        ossClient.shutdown();
    }

Copy the code

Five, the conclusion

OSS is suitable for storing attachments, hd pictures, audio and video files, and backup files of forum websites and software applications, as well as files of various apps, multi-terminal synchronization software, and web disk download sites. For more information, please refer to relevant documents and find them.