Home  >  Article  >  Java  >  Java quickly connects to Huawei Cloud OBS to implement object storage

Java quickly connects to Huawei Cloud OBS to implement object storage

PHPz
PHPzOriginal
2023-07-05 21:17:233296browse

Java quickly connects to Huawei Cloud OBS to implement object storage

With the rapid development of cloud computing, object storage has become an increasingly popular data storage method. Huawei Cloud OBS (Object Storage Service), as a core service of Huawei Cloud, provides highly reliable, low-cost, and scalable cloud storage solutions. This article will introduce how to use Java language to connect to Huawei Cloud OBS to implement common operations such as uploading, downloading, and deleting objects.

Before we start, we need to prepare the following materials:

  1. Huawei Cloud account and activate OBS service;
  2. Java development environment (such as IDEA, Eclipse);
  3. OBS Java SDK can be downloaded from the Huawei Cloud official website.

1. Create a project and import OBS SDK

  1. Create a new Maven project in the Java development environment;
  2. In the project's pom.xml Add the following dependencies to the file:
<dependency>
    <groupId>com.obs</groupId>
    <artifactId>obs-java-sdk</artifactId>
    <version>3.20.3</version>
</dependency>
  1. Save the file and wait for Maven to automatically download the dependencies.

2. Configure Huawei Cloud OBS connection information

  1. Create Access Key ID and Secret Access Key in Huawei Cloud Console for authentication;
  2. Create an OBS instance and record the Endpoint address.

3. Write Java code to implement object storage function

The following is a simple Java code example that implements the upload, download and delete operations of OBS objects.

import com.obs.services.ObsClient;
import com.obs.services.model.*;

public class OBSExample {

    private static final String endPoint = "https://obs.cn-north-4.myhwclouds.com";
    private static final String accessKeyId = "your-access-key-id";
    private static final String secretAccessKey = "your-secret-access-key";
    private static final String bucketName = "your-bucket-name";

    public static void main(String[] args) {
        ObsClient obsClient = new ObsClient(accessKeyId, secretAccessKey, endPoint);

        try {
            // 创建存储桶
            obsClient.createBucket(bucketName);

            // 上传对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "example.txt","Hello OBS!");
            obsClient.putObject(putObjectRequest);

            // 下载对象
            ObsObject obsObject = obsClient.getObject(bucketName, "example.txt");
            byte[] content = obsObject.getObjectContent().readAllBytes();
            String message = new String(content);
            System.out.println(message);

            // 删除对象
            obsClient.deleteObject(bucketName, "example.txt");

        } catch (ObsException e) {
            System.err.println("Error message: " + e.getErrorMessage());
        } finally {
            obsClient.close();
        }
    }
}

In the code, you need to replace your-access-key-id and your-secret-access-key with your Huawei Cloud Access Key ID and Secret Access Key. Also, replace your-bucket-name with the bucket name you created.

This code implements the following functions:

  1. Creates an OBS client instance and connects to the Huawei Cloud OBS service;
  2. Creates a storage bucket. If the storage If the bucket already exists, it will not be created;
  3. Use the PutObjectRequest object to upload an object named "example.txt" to the bucket;
  4. Use The getObject method downloads the object named "example.txt" in the bucket and outputs the content to the console;
  5. Use the deleteObject method to delete the object named "example.txt" in the bucket. The object of "example.txt";
  6. Close the OBS client.

The above code can be modified according to business needs to achieve more object storage related functions.

4. Summary

This article introduces how to use Java language to quickly connect to Huawei Cloud OBS to implement object storage. By using the Java SDK provided by Huawei Cloud, we can easily implement operations such as uploading, downloading, and deleting objects. Readers can further expand the code functions and implement more advanced operations according to their own business needs.

Readers need to be reminded that using OBS services requires following Huawei Cloud’s service agreements and best practices to ensure data security and reliability.

The above is the detailed content of Java quickly connects to Huawei Cloud OBS to implement object storage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn