Home >Java >javaTutorial >Java quickly connects to Huawei Cloud OBS to implement object storage
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. Create a project and import OBS SDK
<dependency> <groupId>com.obs</groupId> <artifactId>obs-java-sdk</artifactId> <version>3.20.3</version> </dependency>
2. Configure Huawei Cloud OBS connection information
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:
PutObjectRequest
object to upload an object named "example.txt" to the bucket; The getObject
method downloads the object named "example.txt" in the bucket and outputs the content to the console; deleteObject
method to delete the object named "example.txt" in the bucket. The object of "example.txt";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!